Check For Zero Byte File Script

How to check for zero byte file in UNIX?

I want to check whether if a file is a zero byte.

in if else condition 

Which option should I use?

is it 

-s 

or 

-z -- is this a valid option 

Cannot find that in unix man for IF 

Solution:

I always use -s ... 

*if [[ -s /x/y/z/file ]]; then* 
* ...* 
*fi* 

Always works. 

or

See man page of bash(see below) 

-s file 
True if file exists and has a size greater than zero. 

-z string 
True if the length of string is zero. 

-s for file size 

-z for string size 

or

You can man test instead of if 

-s file True if file exists and has a size 
greater than zero. 

-f file True if file exists and is a regular 
file. Alternatively, if /usr/bin/sh 
users specify /usr/ucb before 
/usr/bin in their PATH environment 
variable, then test will return true 

File exists and is zero byte 

if [[ -f $file ]] && [[ ! -s $file ]]; then 
echo "File is empty" ; 
fi 

Unix Tips

See Also
Redirecting Output To File

Have a Unix Problem
Unix Forum - Do you have a UNIX Question?

Unix Books :-
UNIX Programming, Certification, System Administration, Performance Tuning Reference Books

Return to : - Unix System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.