Operating System - HP-UX
1844003 Members
2515 Online
110226 Solutions
New Discussion

Checking file size from unix script

 
SOLVED
Go to solution
Carey Goad
Occasional Advisor

Checking file size from unix script

Is there a way to check the size of a file from a script? I have written a filewatch program (using if -f...); however, sometimes a zero byte file gets created. The size command does not seem to work either as it says "bad magic" when attempted.

Thanks
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor
Solution

Re: Checking file size from unix script

Carey:

Use the '-s' option of the test command. This will return true if the file exists and has a size greater than zero.

if [ -s /tmp/myfile ]
then
echo "file size > 0"
fi

...JRF...
Stefan Farrelly
Honored Contributor

Re: Checking file size from unix script


in your script set a variable, eg;

SIZE_OF_PASSWDFILE=`ls -l /etc/passwd | awk '{print $5}'`
(note, use backquotes first and last, use forward quotes around the awk command)

if [ $SIZE_OF_PASSWDFILE -gt 20 ]
then
echo /etc/passwd > 20 bytes
fi
Im from Palmerston North, New Zealand, but somehow ended up in London...
Rick Garland
Honored Contributor

Re: Checking file size from unix script

The -s option to the test will will return true if the file exists and is greater than 0 bytes
James R. Ferguson
Acclaimed Contributor

Re: Checking file size from unix script

Carey:

Also:

# X=`wc -c /tmp/xxx|awk '{print $1'}`
# echo $X #...or compare size, or whatever...

...JRF...