r/ScriptSwap • u/molten • Nov 03 '12
[Request] duplicate file deleter
I have somewhere in the realm of 40k files that have been duplicated into their folders and others. I was hoping for some advice before I rage quit (sledge hammer) on my hard drive.
for clarity's sake, they're all music files, under one directory. They've been pushed and shoved by Rhythmbox, so i'd prefer a bash solution if at all possible.
2
u/ajrisi Nov 03 '12
Check out fslint. You want exactly what it does.
edit: The gui is pretty good too. fslint-gui
2
3
u/terremoto Nov 03 '12 edited Nov 03 '12
Here you go:
#!/bin/bash
rm -f sizes checksums
echo "Sorting by size..."
find -type f -exec stat --printf '%16s %n\0' {} \; | sort -nz > sizes
touch checksums
echo "Comapring files..."
uniq -zd --all-repeated=separate -w 16 sizes | while IFS= read -r -d '' line
do
if [[ -z "$line" ]]
then
# File sizes changed, purge the checksums list
> checksums
continue
fi
path=$(cut -c18- <<< "$line")
escaped_path=$(/bin/ls -b "$path")
echo -n "[ ] $escaped_path" && tput cr && tput cuf1
checksum="$(md5sum "$path" | cut -c-32)"
if fgrep -qf checksums <<< "$checksum"
then
echo "!"
# rm "$path"
else
echo "-"
echo "$checksum" >> checksums
fi
done
rm -f sizes checksumsRun the script in the directory containing the duplicates. Uncomment the # rm "$path" line to actually delete the duplicates. In the output, ! means a duplicate was found.
1
0
Nov 05 '12
[deleted]
1
u/molten Nov 05 '12
Thanks, I'll add this to the list of sub I didn't know about probably because I neglected the sidebar.
2
u/ooldirty Nov 03 '12
Should be easy enough, but it may take more than a few lines.
I would start with (find . -type f -exec md5sum {} ; | awk '{i[$1]++} END {for (a in i) print i[a], a}' | sort -g) to gather your list of duplicates, and then go from there.