How to find and delete duplicate files within the same directory?
8
votes
7
answers
19355
views
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
Asked by Su_scriptingbee
(319 rep)
May 28, 2017, 02:41 PM
Last activity: Oct 24, 2023, 04:15 PM
Last activity: Oct 24, 2023, 04:15 PM