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


clear

echo "Enter Dirctory : \c"
read dir1

# check inputed name is directory or not
if [ ! -d $dir1 ]
then
    echo "DIRECTORY [ $dir1 ] NOT EXIST"
    exit 1
fi

#nevigate to given directory
cd $dir1

# create list of element we have in inputed directory
`echo ls`>fileList

#counter for sub directory
d=0

#counter for zero sized file
f=0

for fileName in `cat fileList`
do
    if [ ! -d $fileName ]
    then
        #find the size of file
        size=`ls -s $fileName`
        size=`echo $size|cut -d " " -f 1`

        if [ $size -eq 0 ]
        then
            f=`expr $f + 1`
            rm $fileName
        fi
    else
        # delete sub directory

        d=`expr $d + 1`
        rm -r $fileName

    fi
done

t=`expr $f + $d`

echo "Total Deleted element : "$t
echo "Total Deleted file : "$f
echo "Total Deleted Sub directory : "$d

1 comments:

Unknown said...

This definition can also be implemented in this way also.....



clear
# c character (unbuffered) special
#f regular file
for i in `find /home/student/Desktop/OS-2 -type f -size 0c`
do
echo "$i has Zero size File"
echo `rm $i`
done

Blog Archive

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.