Structure Query Language, C programming, Java, Servlet, Jsp, Unix

Wednesday, 22 February 2012

Write a script to delete zero sized files from a given directory (and all its sub-directories).


[[  METHOD 1  ]]

clear
`echo ls`>filelist
echo "==============="
total=`cat filelist | wc -w`
echo "total = $total"
i=$total
while [ $i -ge 1 ]
do
# temp=`expr $total - 1`
file=`tail -n $i filelist | head -n 1`
i=`expr $i - 1`
size=`ls -s $file|cut -c 1-2`
# echo "$size"
if [ $size -eq 0 ];then
echo "$file"
rm $file
fi
done

[[  METHOD 2  ]]

clear
`echo ls`>filelist
echo "==============="
for filename in `cat filelist`
do
if [ ! -d $filename ];then
size=`ls -s $filename`
size=`echo $size|cut -d " " -f 1`
echo $size
if [ $size -eq 0 ];then
echo "Want to Remove $filename:"
rm -i $filename
fi
fi
done

No comments:

Post a Comment