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"

-e File exists or not

File size

ls -l sample_file |awk '{print $5 }'

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

keep it simple.

find /dir -type f -name "myfile" -size +0 -exec gzip <fill in the blank> {} \;

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"

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

Hi all,
Please tell me to solve this issue,

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}'` #......error......#
LIMIT=0

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

fi

done
fi
done
}

rotate_files "$ROTATE_FILES"

-----------------------------------------------------------------

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

In case the script is executed from the different directory,then "$i" will expand to MQ.log and not the full path to it,so it throws an error.Either you can try running the script from the directory (ROTATE_PATH) or give the full path

Thanks
Nagarajan G

change -ge to -gt

check if [[ ! -f $i ]];then
only check the size.