To remove files with 0 File Size

Dear All,

I want to remove files having size of (0) that are generated once script is completed.

Here is the code

but i m not sure about /usr/bin

do i need to write /usr/bin, or the path of my script

/tmp/test

please find below the script logic

 
#!/usr/bin/ksh
for i in `ls -a`
do
  FILESIZE=`ls -l $i|awk '{print $5}'`
  if [ $FILESIZE -eq 0 ]
  then
    /usr/bin/rm $i
  fi
done
 

as /usr/bin contains all executable files.

use find command its easy

find . -size 0 -exec rm {} \;

or if you want to use loop
use test command inside your for loop gothrough the man page of test
test ! -s filename

Thnkz vidyadhar85

I recieve followng output

find: 0652-018 An expression term lacks a required parameter

Can u please write the complete line for this case.

one extra sapce was introuduced between -exec rm {} \ ;
try like this

 find . -size 0 -exec rm {} \;

There should be no space between the backslash and the semi-colon.

More efficiently:

find . -size 0 -exec rm {} +

with this you cam enter a size of your desire.
here all below 478 bytes get deleted:

size=0
size=`ls -l "file.txt | awk -F" " '{ print $5 }'`
if [ $size -gt 478 ];
then ((d++));
fi

[b]When you post code, please wrap it in

 tags.



size=0
size=`ls -l "file.txt | awk -F" " '{ print $5 }'`
if [ $size -gt 478 ];
then ((d++));
fi

[/quote]

[indent]
Do you really propose calling two external programs (ls and awk) for every file?

(And you're missing a double quote.)

[quote=cfajohnson;302325344]

[INDENT]
[b]When you post code, please wrap it in

 tags.

Do you really propose calling two external programs (ls and awk) for every file?

(And you're missing a double quote.)


ahh yes, thank you... 

When you are creaing textfiles, one by one, giving all files a number always +1 ... you maybe dont't wan't to save files matching a size criteria, so you don't get gaps in the numbering, then maybe that solution is better.

If you built in

find . -size 0 -exec rm {} +

into a script, allways all files that match this criteria, are gone, even your script, if it does aswell.

If you have a one command slim version for this, I can built it in my scripts: :smiley:

size=0
size=`ls -l "file.txt | awk -F" " '{ print $5 }'`
if [ $size -gt 478 ];
then ((d++));
fi

Where do we miss double quotes, this works like described above.

btw, can it be that if I preview a message and save afterards, I loose the CODE-Tags?

for i in *;do
if [ ! -s $i ];then
rm $i
fi
done

Of course. That was the question that was asked. And it is the same no matter what size you use or whether you use \; or +.

If you want a different size, that can easily be accommodated:

size=$1
find . -size "$size" -exec rm {} +

Why do you set size to 0 when you redefine it on the next line?

Why are you using two external commands (ls and awk)?

size=$( wc -c < file.txt)

If it worked for you, you didn't run what you posted.

You have an unmatched double quote: "file.txt

"loose"??? Do you mean "lose"?

You lose all quotes but the last level. (Previewing has nothing to do with it.)

-----Post Update-----

Your script will fail if any filenames contain spaces.

if [ ! -s "$i" ];then
  rm "$i"
fi

Why is the post being appended to this post when I was replying to a different post?