1827730 Members
2917 Online
109968 Solutions
New Discussion

shell script!!

 
SOLVED
Go to solution
ajay_34
Occasional Advisor

shell script!!

hi all,
i want to write a shell script thats should check size of the given file name. if the file contains zero bytes, remove that file. so any one plz help me. thanks in advance.
ajay.
12 REPLIES 12
NiCK_76
Respected Contributor

Re: shell script!!

find /yourpath -size 0 -exec rm -f {} \;
just for fun
ajay_34
Occasional Advisor

Re: shell script!!

hi nick,
it helped me.thanks for your reply.but i want to use 'if' condition or 'test' command to check the file size and then it should remove the empty files. thanks and regards.
ajay
NiCK_76
Respected Contributor

Re: shell script!!

try following command
find ./ -size 0 -exec rm -i {} \;
just for fun
Georg Tresselt
Honored Contributor

Re: shell script!!

If you insist on using an if, -s is what comes closest to your demand. [ -s ] checks if a file exists and is larger than 0. So you would have to delete all files that fail that test.
http://www.tresselt.eu
Deepak_22
New Member
Solution

Re: shell script!!

#!/bin/sh
File_List=`ls`
for File_Name in $File_List
do
File_Size=`ls -ld $File_Name | awk '{ print $5}'`
if [ $File_Size == 0 ] ; then
echo $File_Name::$File_Size
rm -i $File_Name
fi
done
Ross Minkov
Esteemed Contributor

Re: shell script!!

From the bash man page (man bash):

Conditional expressions are used by the [[ compound command and the test and [ builtin commands to test file attributes and perform string and arithmetic comparisons.

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

Regards,
Ross
ajay_34
Occasional Advisor

Re: shell script!!

thanks deepak.this is the script what exactly i was looking for.
thanks to all who given me suggestions.

regards,
ajay
Stuart Browne
Honored Contributor

Re: shell script!!

Georg and Ross actually gave you a better solution.

If using bash, certainly [[/]] with '-s' is the fastest solution, but if you want multi-platform, it's not the best.

The 'test' ([) application has the '-s' option as well, and it is fully multiplatform:

if [ -s /file/name.here ]
then
rm -f /file/name.here
fi
One long-haired git at your service...
ajay_34
Occasional Advisor

Re: shell script!!

hi browne,

i tryed your script but it dint work browne .y i dont know.it cant delete the empty file. anyway thanks.

ajay
Ross Minkov
Esteemed Contributor

Re: shell script!!


Ajay,

Try this code below. I think Stewart missed one "!".

==============================
#!/bin/bash
if [ ! -s emptyfile ]
then
echo "deleting emptyfile"
rm -f emptyfile
fi
===============================

Again from the bash man page (man bash):

Conditional expressions are used by the [[ compound command and the test and [ builtin commands to test file attributes and perform string and arithmetic comparisons.

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


HTH,
Ross
Stuart Browne
Honored Contributor

Re: shell script!!

yup, I screwed up, my apologies.
One long-haired git at your service...
ajay_34
Occasional Advisor

Re: shell script!!

hi Ross Minkov and Stuart Browne,
thanks for your support. your suggestions helped me.slowly i am learning shell scripting.your suggestions helped me to think in different way.

thanks once again

regards,
ajay