The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
check if remote file exists hcclnoodles Shell Programming and Scripting 2 08-27-2008 02:53 PM
Need to write a script in UNIX to find a file if another file exists mmdawg Shell Programming and Scripting 1 05-04-2008 07:40 PM
Check File Exists and compare to previous day file script rbknisely Shell Programming and Scripting 3 02-07-2008 07:53 AM
Need Script to check whether user exists in the remote machine Srini75 SCO 1 09-07-2005 08:23 AM
file exists and size greater that zero methos Shell Programming and Scripting 3 03-25-2002 09:44 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-04-2008
Registered User
 

Join Date: Feb 2008
Posts: 4
Stumble this Post!
unix script to check whether particular file exists and to find its size

I want to find the size of particular file exists in a particular directory
and i wnt to zip it.
In the below mentioned code it should check the MQ.log in the
particular directory.Please correct my code so that it will check
for particular MQ.log but i could not able to check whether
the MQ.log exist and please tell me the way to check the file size also

ROTATE_FILES="/apps/Mesa/Logs/AE/AE_logs/test"
REAL_FILE="MQ.log"

function rotate_files()
{
for j in $1;do
if [ -d $j ];then
for i in `ls -a /$j`;do

if [ $i=MQ.log ];then
echo $i

$FILESIZE = `ls -l $i|awk '{print $5}'`
$LIMIT = 0

if [ $i -ge 0 ];then
echo "the size is greater"
fi
fi

done
fi
done
}

rotate_files "$ROTATE_FILES"

Last edited by Balachandar; 02-04-2008 at 02:32 AM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-04-2008
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,572
Stumble this Post!
-e File exists or not

File size
Code:
ls -l sample_file |awk '{print $5 }'

Last edited by matrixmadhan; 02-04-2008 at 02:45 AM.
Reply With Quote
  #3 (permalink)  
Old 02-04-2008
Registered User
 

Join Date: Feb 2008
Posts: 4
Stumble this Post!
unix script to check the file size

please tell me in my case how i can modify the code
Reply With Quote
  #4 (permalink)  
Old 02-04-2008
Registered User
 

Join Date: Aug 2007
Location: Bangalore
Posts: 285
Stumble this Post!
Quote:
Originally Posted by Balachandar View Post
please tell me in my case how i can modify the code
if [[ -e $filename ]];then
echo "file exists"
fi

or to check if its a file not a dir then

if [[ -f $filename ]];then
echo "its a simple file"
fi

if you are taking as command line arguements replace $filename with $1,if it is the firsta rguement.

filesize=`ls -l sample_file |awk '{print $5 }'`
echo $filesize
Reply With Quote
  #5 (permalink)  
Old 02-04-2008
Registered User
 

Join Date: Sep 2006
Posts: 1,542
Stumble this Post!
Quote:
Originally Posted by Balachandar View Post
I want to find the size of particular file exists in a particular directory
and i wnt to zip it.
keep it simple.
Code:
find /dir -type f -name "myfile" -size +0 -exec gzip <fill in the blank> {} \;
Reply With Quote
  #6 (permalink)  
Old 02-04-2008
Registered User
 

Join Date: Nov 2007
Posts: 52
Stumble this Post!
Hi Balchander

I think you can do it this way

ROTATE_FILES=" <PATH>"
REAL_FILE="MQ.log"

function rotate_files()
{
for j in $1;do
echo "Loop1"

if [ -d $j ]
then
for i in `ls -a /$j`
do

if [ $i = MQ.log ]
then
echo $i

FILESIZE=`ls -l $i|awk '{print $5}'`
LIMIT=0

if [ $FILESIZE -ge 0 ]
then
echo "the size is greater"
fi

fi

done
fi
done
}

rotate_files "$ROTATE_FILES"
Reply With Quote
  #7 (permalink)  
Old 02-04-2008
Registered User
 

Join Date: Feb 2008
Posts: 4
Stumble this Post!
bug in checking the file size

Thanks infyanurag and all

the code works fine but i still get some bugs.......
Its printing the loop1 and MQ.log but when it goes to checking
the file size it throws error...... "ls: MQ.log: No such file or directory" .........
and also in the braces which suppose to check the file size greater than
zero

Inside the script
FILESIZE=`ls -l $i|awk '{print $5}'`
"ls: MQ.log: No such file or directory" <<<error message

if [ $FILESIZE -ge 0 ]
"[: -ge: unary operator expected" <<<error message

please tell me how to solve this
--------------------------------------
Loop1
MQ.log
ls: MQ.log: No such file or directory
test4.sh: line 21: [: -ge: unary operator expected
-------------------------------------------

please correct me

Last edited by Balachandar; 02-04-2008 at 05:25 AM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:20 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0