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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting zero byte files 2

Status
Not open for further replies.

icu812

MIS
Sep 10, 2001
52
US
Can someone please tell me how to get a script to delete all zero byte files within a directory?? Thanks
 
From the directory in which the files are, type:

find . -s 0 -print

will show you a list of the files in question.

Once you're satisfied that you really want to delete all of them replace print with exec as follows:

find -s 0 -exec rm {} \;

The -s 0 option indicates 'shaow me all files of zero bytes exactly'. Hope this helps
 
Using a kshell, you can test for a file of greater than 0 bytes using if -s.

#!/bin/ksh
cd - to your directory
for filename in `ls -1`
do
if [[ -s $filename ]] then
echo "Do nothing"
else
echo "Removing file"
rm $filename
fi
done

Take care when setting up ant script which uses rm - test it thoroughly on dummy files before using it live.


 
Just noticed, the second find in my response is missing a . (period) as in find . -size 0 -exec rm {} \;

Don't forget that if there's not too many of them, it might be worth including the -i (interactive) option after rm so that you are prompted that you really (really) want to delete the file. Cheers.
 
I think is dangerous to use [tt]find [red].[/red][/tt], better use
[tt]
find /the/directory -size 0 -exec rm {} \;
[/tt]

I hope it works...
Unix was made by and for smart people.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top