postheadericon Write a script to display the name of those files (in the given directory) which are having multiple links.

 

clear
echo "Enter Directory : "
read dir

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

#count total number of lines
len=`ls -l $dir | wc -l`
i=2

echo "File With Multiple Link are "

while [ $i -le $len ]
do
# select one by one record from entire group of records
record=`ls -l $dir | head -n $i | tail -n 1`

#grab the file name
f=`echo $record | cut -d " " -f 8`

#grab the total hard link to that file
link=`echo $record | cut -d " " -f 2`

#if multiple links then print file with link
if [ $link -gt 1 ]
then
echo "$f = $link"
fi

#increment the counter
i=`expr $i + 1`
done

1 comments:

Jigar Shah said...

this code can be done in this way also...

clear
i=2
ls -l > 1.txt
l=`ls -l | wc -l`
#ln -L oldfile newfile
echo "========================"
echo "Multiple links file name"
echo "========================"
while [ $i -le $l ]
do
a=`ls -l | head -$i | tail -1 | cut -c 12`
b=`ls -l | head -$i | tail -1 | cut -c 55-61`
if [ $a -ge 2 ]
then
echo "File name is :$b"
fi
i=`expr $i + 1`
done
echo "========================"

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.