Remove all duplicate songs/mp3s iTunes creates using bash script
Alec Jacobson
September 22, 2009
I gave up using iTunes to play music about year ago, but I haven't found a free alternative to iTunes's exceptional file management based on mp3 ID3 tags (If you know of one --- I mean better than iTunes one --- let me know). So occasionally I let iTunes organize my music library. I drop in folders containing new music and let iTunes go at it. The problem is that if I have folders containing an mp3 and an m3u playlist I get duplicates. If I don't notice this right away the duplicates build up.
Here's a bash script to delete all true duplicates. The files must be exactly the same and have almost the same name (the difference being the number iTunes appends on a copy: "song.mp3" becomes "song 1.mp3").
Verbose version:
#!/bin/bash
find "$1" -regex ".* [0-9][0-9]*\.[^.]*" -print0 | while read -d $'\0' copy
do
original=`echo "$copy" | sed "s/\(.*\) [0-9][0-9]*\(\.[^.]*\)/\1\2/"`
# check that the hypothetical original exists
if [ -e "$original" ];then
# check that the original is exactly the same file as the copy
if diff "$original" "$copy" >/dev/null ;then
rm "$copy"
echo "$copy deleted..."
else
echo "$copy is different..."
echo " $copy not deleted..."
fi
else
echo "$original does not exist..."
echo " $copy not deleted..."
fi
done
Quiet Version
#!/bin/bash
find "$1" -regex ".* [0-9][0-9]*\.[^.]*" -print0 | while read -d $'\0' copy
do
original=`echo "$copy" | sed "s/\(.*\) [0-9][0-9]*\(\.[^.]*\)/\1\2/"`
# check that the hypothetical original exists
if [ -e "$original" ];then
# check that the original is exactly the same file as the copy
if diff "$original" "$copy" >/dev/null ;then
rm "$copy"
fi
fi
done