Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete a file if 0 bytes

Status
Not open for further replies.

beaster

Technical User
Aug 20, 2001
225
US
I need to add to my script, the function to delete a file if it is in fact a file size of zero. The file name is alarm_sms.txt

Can anyone help out?

Thanks,
Beaster
 
Use this test (negated) :-
-s filename

True if filename exists and has a size greater than zero.

Eg:
# Check file exists ...
if [ -f file ]; then
# Check it's size is not greater than 0 ...
if [ ! -s file ]; then
rm file
fi
fi

One by one, the penguins steal my sanity.
 
I used this in my script, before as response was listed here: What do you think?

find /home/beaster/alarm_program/alarm_sms -size 0 -exec rm {} \;
 
It works, but it's a bit like using a sledgehammer to crack a nut. You can achive the same by saying ...

[ -s /home/beaster/alarm_program/alarm_sms ] || rm /home/beaster/alarm_program/alarm_sms

Greg.
 
Assuming the file alarm_sms.txt is in the directory /home/beaster/alarm_program, then I'd try :-

find /home/beaster/alarm_program -name "alarm_sms.txt" -size 0 -exec rm {} \;
One by one, the penguins steal my sanity.
 
Thanks, they all work great.

//beaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top