Fixing iTunes When It Forgets the Hard Disk With All Your Music
I have all my music on a hard disk, and occasionally iTunes resets the music library location to my home folder - just like that, without asking. So once again I find myself having to fix iTunes, because it can’t find the songs and it shows me the dreaded exclamation marks instead. Here’s what I do to fix it.
Disclaimer: If you follow the instructions on this post, you do so at your own risk. I suggest you backup all the files involved.
Reset the iTunes Library
Open the iTunes music folder in Finder ( it’s in your home folder / Music / iTunes) and drag iTunes Music Library.xml and iTunes Library.itl out on the desktop. This will reset iTunes next time you start it, but don’t start it yet. We’ll be working on the xml file, whilte the itl file is just there for backup.
Change the location of where iTunes keeps the music
Has iTunes changed the location from your hard disk to your home folder? Bastard. In the iTunes preferences > Advanced, click on “change” next to the “iTunes music folder location”, and select your hard disk. I leep “Keep music folder organized” unticked.
Look inside iTunes Liberary to see what the problem is
If your iTunes library is huge, it’s a good idea to use Terminal to look at the first few lines, which you can do easily with
(you can just type “head -n100 ” and drag the file onto Terminal to let OS X type out the name for you)
The output will be something along the lines of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
You need to look out for the file:// bit of the xml. Here it is telling me that it is looking into /Users/ME/Music/, i.e. my home folder instead of my external hard disk. Bastard.
Use sed to fix the path and create a new xml file
sed is a nifty unix utility which you can use from Terminal. It can open a file, go through it line by line, change things on each line, and then put the result in another file. At least, that’s how I am using it here.
Here’s a breakdown of what the command does.
- calls the sed command - it stands for Stream EDitor
- sed will itself need instructions, which are strings hence the ’ (apostrophe). s/a/b/ means “if you find an a, substitue it with b”
- this is the string we are looking for. Note that given that forward slashes are used by sed for s/…/…/, if you need them in the string you have to escape them with backward slashes
- the string we are replacing with
- < /Users/ME/Desktop/iTunes\ Music\ Library.xml
- The less than sign means “use this file as input” - in this case, the xml file I had dragged to the Desktop
- > /Users/ME/Music/iTunes/iTunes\ Music\ Library.xml
- and this means “create this file as output”. We are asking sed to recreate the xml library file from the one on the desktop, but changing some bits around as it does so
Create a broken iTunes library
Now this is important - if you just start iTunes now, with the new .xml file there and no .itl file there, nothing will happen. iTunes will ignore the xml file, and just reset to an empty library. But if iTunes finds a broken .itl file, it will use the .xml file we just fiddled with to recreate the .itl file. The easiest way to create a broken .itl file is from terminal:
which will create an empty file - broken enough for iTunes.
Now iTunes will finally pay attention to the xml file - it will show a dialog saying ‘Importing ‘iTunes Music Library.xml’…” This may take a while, even though it isn’t actually copying mp3 files around.