I want to find duplicate files, within a directory, and then delete all but one, to reclaim space. How do I achieve this using a shell script?
For example:
pwd
folder
Files in it are:
log.bkp
log
extract.bkp
extract
I need to compare log.bkp with all the other files and if a duplicate file is found (by it's content), I need to delete it. Similarly, file 'log' has to be checked with all other files, that follow, and so on.
So far, I have written this, But it's not giving desired result.
#!/usr/bin/env ksh
count=`ls -ltrh /folder | grep '^-'|wc -l`
for i in `/folder/*`
do
for (( j=i+1; j<=count; j++ ))
do
echo "Current two files are $i and $j"
sdiff -s $i $j
if [ `echo $?` -eq 0 ]
then
echo "Contents of $i and $j are same"
fi
done
done
echo $?
is equivalent to$?
. Also$(command)
is preferred to using back ticks.md5sum `find -type f` | sort | uniq -D -w 32
(superuser.com/questions/259148/…) It does not automatically delete duplicates but it could be changed if you really want to.