Fixing iTunes when it forgets the hard disk with all your music

itunes, sed
This post is too old to be of any use. It has been archived.

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

head -n100 /Users/ME/Desktop/iTunes\ Music\ Library.xml

(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

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "">
    <plist version="1.0">
        <key>Major Version</key><integer>1</integer>
        <key>Minor Version</key><integer>1</integer>
        <key>Application Version</key><string>10.6.3</string>
        <key>Music Folder</key><string>file://localhost/Volumes/HD1T/Music/</string>
        <key>Library Persistent ID</key><string>C326B89DCF4457DF</string>
                <key>Track ID</key><integer>41557</integer>
                <key>Name</key><string>Fifths (Jazzanova 6 Sickth Mix)</string>
                <key>Album</key><string>Jazzanova: The Remixes, 1997-2000</string>
                <key>Kind</key><string>MPEG audio file</string>
                <key>Total Time</key><integer>482925</integer>
                <key>Disc Number</key><integer>2</integer>
                <key>Disc Count</key><integer>2</integer>
                <key>Track Number</key><integer>1</integer>
                <key>Track Count</key><integer>10</integer>
                <key>Date Modified</key><date>2008-12-05T14:26:06Z</date>
                <key>Date Added</key><date>2012-07-14T07:42:37Z</date>
                <key>Bit Rate</key><integer>320</integer>
                <key>Sample Rate</key><integer>44100</integer>
                <key>Play Count</key><integer>12</integer>
                <key>Play Date</key><integer>3321087305</integer>
                <key>Play Date UTC</key><date>2009-03-28T10:15:05Z</date>
                <key>Album Rating</key><integer>80</integer>
                <key>Album Rating Computed</key><true/>
                <key>Artwork Count</key><integer>1</integer>
                <key>Persistent ID</key><string>E5B52C3DE807B7DC</string>
                <key>Track Type</key><string>File</string>
                <key>File Type</key><integer>1297106739</integer>

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.

    sed 's/file:\/\/localhost\/Users\/ME\/Music\/iTunes\/iTunes%20Media\//file:\/\/localhost\/Volumes\/HD1T\//'  < /Users/ME/Desktop/iTunes\ Music\ Library.xml  > /Users/ME/Music/iTunes/iTunes\ Music\ Library.xml

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:

touch /Users/ME/Music/iTunes/iTunes\ Library.itl

which will create an empty file - broken enough for iTunes.

Restart 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.