unix script to check whether particular file exists and to find its size


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting unix script to check whether particular file exists and to find its size
# 1  
Old 02-04-2008
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 06:32 AM..
# 2  
Old 02-04-2008
-e File exists or not

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


Last edited by matrixmadhan; 02-04-2008 at 06:45 AM..
# 3  
Old 02-04-2008
unix script to check the file size

please tell me in my case how i can modify the code
# 4  
Old 02-04-2008
Quote:
Originally Posted by Balachandar
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
# 5  
Old 02-04-2008
Quote:
Originally Posted by Balachandar
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> {} \;

# 6  
Old 02-04-2008
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"
# 7  
Old 02-04-2008
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 09:25 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script which will check the target file and folder exists and copy it

Hi All, I am a beginner in this and trying to write a shell script in linux which will : 1. Ask for a file name and check if its exists. 2. If file exists only then it will ask for the new target folder, after entering target folder name it will check if it exists. 3. If target folder... (3 Replies)
Discussion started by: ashish_neekhra
3 Replies

2. Shell Programming and Scripting

Except script fails to check file exists or not in remote node

Dear members, The following expect script connects to remote node and check for the file "authorized_keys" in directory /root/.ssh in remote node. However the result is always found even if the file exist or doesn't exist. expect { "$fname" { send_user "found\n" } Any idea what is... (4 Replies)
Discussion started by: Sudhakar333
4 Replies

3. Shell Programming and Scripting

Script to check file exists

Hi, I am trying to write a script which checks if any file exists with "*.log" or "*.out" in Directory below is the code #------------------ path=/abd/xyz/ if ; then echo "Good" else echo "Failure" fi #-------------------------- its always going to else part and printing... (8 Replies)
Discussion started by: ch33ry
8 Replies

4. Shell Programming and Scripting

Script to check for the file existence, if file exists it should echo the no of modified days

Hi, I am looking for a shell script with the following. 1. It should check whether a particular file exists in a location #!/bin/sh if ; then echo "xxx.txt File Exists" else echo "File Not Found" fi 2. If file exists, it should check for the modified date and run a command... (2 Replies)
Discussion started by: karthikeyan_mac
2 Replies

5. Shell Programming and Scripting

Script to check if file exists

guys, I am trying to write a script that does the following: it looks for a file in a specific directory and if the file is not there (NOT), it emails me. I have tried the following but its not working. It simply hangs up. Please help. if then mail -s 'blah blah blah' my email... (4 Replies)
Discussion started by: basisvasis
4 Replies

6. Shell Programming and Scripting

Shell script to check if any file exists in 4 folders

Hi All, working on AIX 5.3. Requirement is: Shell script in ksh to check if any file exists in 4 folders as below: 1. /FILE/INB/INT1 2. /FILE/INB/INT2 3. /FILE/INB/INT3 4. /FILE/INB/INT4 Thanks a lot for your time! a1_win. (3 Replies)
Discussion started by: a1_win
3 Replies

7. UNIX for Advanced & Expert Users

how to find the file size in unix

Anybody can help HOW TO FIND THE FILE SIZE IN UNIX (5 Replies)
Discussion started by: lmraochodisetti
5 Replies

8. Shell Programming and Scripting

Command/script to find size of Unix Box ?

Please could anyone provide me the Command/script to find the size and usage of Unix box ASAP ? (6 Replies)
Discussion started by: sakthifire
6 Replies

9. Shell Programming and Scripting

Need to write a script in UNIX to find a file if another file exists

So I have a lot of Java applications on my servers all having their own folder from the applications subdirectory. Now, I need to do the following. Search all the applications subdirectories for message.jar. If the message.jar file exists, I need to search the application directory for... (1 Reply)
Discussion started by: mmdawg
1 Replies

10. Shell Programming and Scripting

Check File Exists and compare to previous day file script

We have data files that are ftp'd every morning to a SUN server. The file names are exactly the same except for that each has the date included in its name. I have to write script to do 2 things: STEP 1) Verify that the file arrived in morning. STEP 2) Compare the file size of the current... (3 Replies)
Discussion started by: rbknisely
3 Replies
Login or Register to Ask a Question