# iTunes Duplicates

Gaa!  I have duplicates in my iTunes, wasting space and filling up my backup.  Boo!  Must fix them.

There is a Find Duplicates (and with the option key, a Find Exact Duplicates) on the View menu in iTunes, but it just finds them.  I want them gone!  I don’t want to click every other file and then say delete, I want the ones that are clearly duplicates gone, gone, gone.

To that end I wrote the following tiny bash script (OS X) to clean up my iTunes folder.  If you want, you can grab it and use it – just don’t blame me if it deletes anything you care deeply about.  I have backups.  You should to.  Do not run without a backup.

Otherwise, enjoy!

#!/bin/bash ## ## Clean up the iTunes folder by looking for exact duplicates and removing them. ## Yes, there is functionality in iTunes to do this, but it leaves it to you to ## actually remove the files. ## ## Disclaimer: This is free software. Use it at your own risk. ##   # Globstar! Not on OS X, I'm afraid. pushd ${HOME}/Music/iTunes >/dev/null unset a i while IFS= read -r -d$'\0' filename ; do   # We might be deleting files, so make sure the one we are looking # at right now does, in fact, exist. if [ ! -e "$filename" ] ; then continue fi # Now we have a file. Grab its MD5. md5_orig=$(md5 -q "$filename") #echo "$md5_orig $filename" # Remove the extension. Because. bn=$(basename "$filename" ".mp3") dn=$(dirname "$filename") # Now look for extensions of that filename with a single digit. for extension in 2 3 4 5 6 7 8 9 ; do # If that extension is found, compute its MD5. If they match, # the DELETE the duplicate. newname="$dn/$bn$extension.mp3" if [ -e "$newname" ] ; then # Found a possible duplicate. Compute its MD5. md5_dup=$(md5 -q "$newname") #echo " ++$md5_dup $newname" # If the hashes match, delete the duplicate. if [$md5_dup == $md5_orig ] ; then /bin/rm -f$newname echo "Deleted \$newname." fi fi done done < <(find . -type f -name '*.mp3' -print0) popd >/dev/null exit 0

stacy

I do whatever it is that I do. Typically that's formal methods in software engineering and security.