I see some discussion about fixing busted iTunes libraries, either when moving one on the same computer or migrating to a new one. Here’s what I have found works for me. Bonus: no slow AppleScripts or payments (donations cheerfully accepted and squandered).
As an aside, if you are going to comment along the lines of “I just moved my music from one Mac to another and nothing broke so why is this so complicated?” please don’t bother. The title specifically mentions a BROKEN library, you dig? Or perhaps your library has outgrown the current location and you want to move it but retain access to it as your primary library and keep all your playlists and history. That’s what this post deals with, not replicating a working installation, but changing one to suit a new environment or fixing a botch attempt at it. Peace.
And for goodness’ sake, read all the comments: some good stuff down there.
First, what I have discovered about how iTunes manages music collections. There are two files it uses, one that is binary (ie, machine readable for faster performance on searching, sorting, add/edit/delete operations) and one that has the same information but in a human readable format (for a certain subset of humans who can read XML natively). The XML file is written from the binary file as a backup (check the dates to confirm).

iTunes will try to open the binary file when it starts up and if it’s not readable, it will re-create it from the XML file. That’s what we’re using to fix the damage or make any sweeping changes.
First, close iTunes. You’ll be working with files it uses/writes to and you’ll muck things up if you makes changes to files it has open and then your changes get clobbered, leaving the binary and the backup unusable. Don’t do that.
- find your Music folder/directory where these files live: you are looking for iTunes Library and iTunes Music Library.xml
The XML file is a backup of the other file, which is the one iTunes uses:
iTunes Library: data[NB for Windows users: you will be looking for iTunes Library.itl and iTunes Music Library.xml.]
iTunes Music Library.xml: XML document text - Move the iTunes Library file aside (rename it, put in your home directory, whatever). Create an empty file with the same name (or simply mangle a copy of it: seriously: this is where the backup copy comes in). If you are in the Terminal, you can just use
touch(1), as intouch iTunes\ Library. You can also just open a file in whatever other editor you like and save it as iTunes Library. - You’ll need to edit the XML file, in a text editor, not a word processor (not Word, as if I have to explain that). vi, emacs, pico, nano, TextWrangler, BBEdit, TextMate on the Mac side, NotePad/WordPad, et al on the Other Side. You are going to search and replace the current location with the new one. Every file/track/song has it’s own stanza in that file and within that, there is a Location key. You need to replace the old one — as an absolute path — with the new one.
An example:<key>Location</key><string>file://localhost/private/Network/Servers/shuttle
/usr/local/share/mt-daapd/media/Abbado_Berliner%20Philharmoniker/Mahler_%20Symphonie%20No.%205/01%20
Trauermarsch.%20In%20Gemessenem%20Schritt.%20Streng.%20Wie%20Ein%20Kondukt.m4a</string>
- A quick and dirty way to do it on the command line, ie, in the Terminal?
perl -pi -e.backup s|oldpath|newpath|g iTunes Music Library.xml
perl -pi.backup -e s|oldpath|newpath|g iTunes Music Library.xmlThis will create a backup file (cleverly called “iTunes Music Library.xml.backup”)
- Now, test it. iTunes needs to find that iTunes Music file and since it can’t read it (it’s empty or damaged), it will create one from the backup you just edited. Give that a minute or three to complete. See if your files are found and playable. This should also keep your playlists (which are just another XML stanza with the elements being track ID numbers).
This has worked for me more than once and if it’s reproducible, it should work for you. But note that I recommend a backup copy of the XML file. You can go back and re-do it from scratch if you get into trouble.
And I expect this is extensible in many interesting ways, like merging libraries from different users (something with diff and patch, perhaps?), switching from one repository to another (maybe a large home repository and a smaller traveling one). I’m not clever enough to figure those out, but if anyone else does, I’d love to hear about it.
[update] This looks interesting:
I had to do some more things with my iTunes library lately – like extracting all that ratings and exporting them into a new music player software I liked to test. I therefore wrote myself a little tool in C# that does the job of reading in the whole iTunes library and giving you programmatically access to that library. It only needs to have read access to the Mediathek.xml file iTunes stores in it’s music folder and you from there on can work your way through the bazillions of music tracks you may or may not have in your library. It even does the find-and-replace job a bit easier than the solution mentioned in the article above.
Huh. Not sure what happened in your case though the reference to “a touched replacement file, as well as empty txt file” confuses me a bit.
And yes, the Date Added stuff gets clobbered. I suppose a smarter person than me (read: anyone) could do some cleverness with the create date of the music file and use that as the Date Added value.
As in I’ve tried
touch itunes libraryand creating an empty text document with the same name.Is case sensitivity an issue here?
I accidentally deleted the ‘recently added’ and ‘recently played’ folders from my iTunes. These were “pre-set” folders by iTunes, so to speak, because I didn’t create the playlists. Is there any way I can get them back? I used the ‘recently added’ folder all the time and would really like to have it again.
Thanks.
Jill
That’s a Smart Playlist. You can just create new ones that meet your requirements.
My hd was very full. I added a new hd to my computer (mac) and moved my itunes folder to the new hd. I redirected my itunes to see the songs in the new hd. My problem is all of my playlist songs do not see the new location. I have to go into the itunes folder in order for itunes to recognize each song. I have over 5000 songs. This is very tedious. There has to be an easier way for the songs in my playlist to recognize the new location of my songs other than finding each one. Please help.
Did you read the instructions or the comments? Can’t make it any easier w/o doing it for you.
Paul–I’m using iTunes with Windows XP, no iPhone.
Suddenly, out of the blue, an empty opening screen. No files in the library.
Can I solve this problem by simply downloading the latest version of iTunes?
I sure hope the answer is yes. David in Bangkok
Probably easily fixed but not that easily ;-)
Worst case, you have to re-import everything again and lose play counts, playlists, history, etc. The first step is to find the data files mentioned in the original post and see what state they are in.
Paul–The files seem to be all okay. I have no problem with re-creating my eight or ten podcast subscriptions.
The sort of under-the-hood tinkering that you talk about at the top of this thread is way, way, way beyond me.
Should I first remove the existing program and then download iTunes8 (or whatever it is)?
Thanks a million for your help. David
I am a real novice when it comes to using terminal and have never tried to edit an xml file before and am confused about step 3. Right now the xml file is searching on my computer’s hard drive but I need to point it to my external.
ex: Locationfile://localhost/Users/bashomosko/Music/iTunes/iTunes%20Music/01-method_man_and_redman-how_h/Unknown%20Album/01-method_man_and_redman-how_high_part_2_(dirty)-ego.mp3
but I’m not sure how to write the replacement to my external hd where my music sits. Also, If I fix the text of the first song in the xml file, save it, and open itunes, will it find the rest of my files automatically? I’m just a bit confused as to whether I have to edit every strand in the xml file to redirect it. Again, pardon my naivete as I am a novice but anxious to get this sorted out.
Thanks!
Basho, you will need to search and replace all instances of that path in the whole file. It won’t find the other files if you only tell it where one is ;-)
Make sure you back up the old files (as noted in the instructions), grab a copy of TextWrangler if you don’t have a good text editor, and see the examples. Come back if you’re still stumped. No need to use the terminal if you don’t want to.
I hooked up an ext HD to my overflowing PC in October. I backed up the 20+G iTunes Library to DVD–9 discs’ worth. (I understand a disc backup contains all metadata?) Then I moved the iTunes Music folder-properly-to the external HD. All was perfect in my iPod world. Then my teenaged son blew up the computer, and a techie friend just reformatted it–didn’t ask first, no attempt at data recovery-like the library database files…soooo I had to download iTunes 8 on this clean old PC, but I’ve done nothing with the new iTunes install yet out of fear. The intact library (pre-vers. 8) sits on the external, but I have no matching .itl/.xml files. Then dear husband buys himself a new Nano, and starts importing CDs–so he’s begun a new library! So…do I delete *everything*, re-install iTunes 8, restore the library backup from the DVDs? I saw your instructions for rebuilding the database files, but I’m…not really understanding the binary thing and the replacing the path, etc. I’ve read everything thus far, but it’s late and I’m cowardly, maybe in the a.m. I’ll reread, and you’ll give me a shove, and…thanks for tolerating this advanced beginner PC user. p.s. if I can salvage the library without too much pain, sans the playlists, I guess I’ll manage but I’m a fitness instructor and there are lots of imported exercise CDs that I had to manually pull into playlists because they all were called “track 01″, etc. TMI, sorry.
thanks, works like a charm! moved a library with playlists and all counts etc from itunes 7.x to 8.1, on xp to vista.
edited the xml file and created a damaged itl file as suggested and poof, magic :)))
Hi all. I’ll cut to the crux of my problem.
I’ve followed the advice above and changed all of the locations of my files. When I do the “find and replace” in iTunes it finds 49k occurrences, which seems about right because I have 45,670 songs in my library. But when I open iTunes and it rebuilds from the .xml file, my library suddenly only has 2,420 songs. The songs that show up aren’t random–they were all added to my library on the same day. (Not the most recent batch of songs I added either.) Also, iTunes has added a smart playlist called “music” that shows all of the 2,420 songs added. I’ve double-checked to make sure all of the music is in the proper folder where I’ve told iTunes to look, and it is. Any ideas why it’s not finding everything?
In the above message, I meant to say that TextWrangler finds 49k changes in the xml file. Sorry.
This also may be of note:
When I open the fixed .xml file in TextWrangler, it shows all 49k of the songs, but once iTunes has opened and automatically re-saved the .xml file, that new file shows only 2420 songs. So something is happening when iTunes creates a library from my .xml library and the app isn’t reading the whole thing.
I’m confused, please can someone dumb this down for me.
Old computer is a Dell PC running on XP. iTunes library I *think* is stored in 2 places… (long story, my iTunes got wiped and I’m not 100% sure where all my files are).
New computer is an iMac (not sure on OS? MEMORY 065-7993 4GB 1066MHZ DDR3 SDRAM – 2X2GB, HARD DRIVE 065-8000 1TB SERIAL ATA DRIVE).
Anyway… I was following the recommendation to consolidate my library in one location. I did NOT have enough room on my Dell to consolidate. I have an external drive and I consolidated everything to THAT drive. I then copied that file that was consolidated onto my new computer (the iMac). I think all the songs moved, but I was under the impression that playlists and play counts could also migrate?
Ouch. This may not have worked as you expected. Copying outside of iTunes, as in just dragging files/folders around, can mess things up. Fortunately, it’s easily remedied with varying degrees of thoroughness: the more times you do things like this, the greater the risk of losing history, play counts, and other metadata. I would back up a step. Take the drive with the consolidated files and add it to the iMac. Now proceed as you have here:
I got stumped at this set of instructions:
PC-Mac – Copy across your library file – Now find the ‘iTunes Library.itl’ file on your Windows PC (it should be in the ‘My Documents – My Music – iTunes’ folder). Copy this across to your Mac’s ‘iTunes’ folder (note: this is not the ‘iTunes Music’ folder but is usually one folder back from that – you have the correct folder if it has the ‘iTunes Library’ and ‘iTunes Music Library.xml’ files in it). Now, delete the ‘iTunes Library’ file on your Mac, and then rename your newly copied across ‘iTunes Library.itl’ file to ‘iTunes Library’ (i.e. remove the .itl from the end). This has given your Mac’s iTunes a new library file pre-populated with all the extras you wanted to keep from your old library.
If I consolidated to an external drive – where do I find this ONE file??
This should be in your home directory. Open the Finder and in the lefthand tab, you should see a Music folder. Open that and open the iTunes folder in there. Your files should be there. Remember the iTunes directory is not necessarily where your music files are and the iTunes database files (confusingly named iTunes Library and iTunes Music Library.xml) are not your music files. But they are the files iTunes needs to have to work.
also, a lot of my artwork seems to be missing.
That should be on the PC: look for a directory called “Album Artwork.” Probably in “My Documents – My Music – iTunes.” Failing that, you can just tell iTunes to re-fetch it all.
Can anyone help? Or refer me to a site that may walk me through this a bit?
Thank you!!
Thanks for the fast response! I did copy the consolidated library folder and simply dragged it onto the iMac.
So… when you say the .xml filed should be in my home directory… am I checking on my external drive or am I “chancing” it and looking for it on my Dell?
Thanks again!
The iMac file will be .xml and the Windows version (on the Dell) would be .itl (for iTunes Library, I assume). And it won’t be on the external drive. That’s where the music files are and the music files are not the library/database files.
So you could shut down iTunes, move the two files on the iMac somewhere (a folder called “old” is fine), move the files from the Dell into place, make sure the names are right, and open iTunes again. Alternately, you could simply move the .itl file and change the .itl to .xml and it may just magically build a new library database.
Hi,
I came across this article from TUAW after some frantic google searching.
Using OS X leopard using iTunes 8.x
iTunes 8 seems to do things a little differently but I’m not sure what, so your mileage will certainly vary.
Plugged in my new external HDD.
Copied the entire /users/Music/iTunes folder to external-drive/media/iTunes
Option clicked on the iTunes icon in the dock and selected the library within the external-drive/media/iTunes folder
iTunes loaded up looking all well and good with all songs and playlists in tact. anytime i actually try and play a song i get “cannot locate file..” message. bummer.
have tried your technique just now of screwing with the “iTunes Library” file and changing all the filepaths within the .xml file.
Are you sure the paths you changed are correct? Double-check that just in case.
upon opening iTunes after the changes, it does indeed say the library is corrupt and uses the .xml file to build the database… so finger crossed..
but when it finishes building the database, all the playlists are there but the music library is only a fraction of the size… only 159mb instead of 70gb!!
clicking on playlists shows no songs as they are not present for some reason in the music library.
when i put the original “iTunes Library” file back in place (after quitting itunes of course), and then re-open iTunes, all songs are present and iTunes reports that I have 70gb music blah blah… although it cannot find any of them.. lol
If you “Get Info” on the missing file, it should give you an option to go find it: might be worth doing that and then comparing a fixed file with one that’s broken to see what difference there may be.
very confused now… please are you able to assist by email please?
many thanks paul
just to update on my situation..
i have managed to copy all external-hd/media/iTunes back to the original location it was on the /users/music/iTunes and also restore the itunes Library & iTunes Library.xml back from time machine… so iTunes is now working again to the same position i was at about 4 hours ago before i started the new external hdd stuff!
so now i want to try the whole thing again, but make it work – but will wait for you advice this time rather than just plough in!! lol
hurry hurry :)
Over the last day or so, I have learnt more about the innards of iTunes than I ever wished to know. I wish I had stumbled upon this ages ago. Completely fixes “the original file could not be found” problem that many people seem to have encountered, and now that I’ve inserted this as a comment will hopefully find this solution.
Thank you very much Paul.
PC. Problem is ugly. was in process of opening iTunes to add downloaded song. Pop up box msg states cannot load iTunes library. I quit. Now says library is damaged. Gives me a new one with what looks like most but not all songs. NO PLAYLISTS! Yes, I cried. Cannot restore to earlier date. Not all on iPod, it’s only 8G….gift giver thought that was enough. (Butthead. Help?
You may want to avail yourself of the instructions. You might be OK if you can make a copy of the backup file before iTunes clobbers it. Quick summary: there are two files, a binary (machine-readable) database file and a text file backup. If the text file backup is OK, iTunes can rebuild the binary file (the one it uses) from that. So check your dates and file sizes in the iTunes directory and cross your fingers that the text file is older than the binary (the filenames escape me at the moment but I think it’s .XML for the file and .itl for the database.
This post got my hopes up! Alas, this doesn’t appear to work in iTunes 9.
I edit my XML file, save it.
I then create a blank .itl file.
When I re-launch iTunes, it just names my itl file as damaged and then opens with a blank library. It doesn’t seem to make an effort to rebuild the library based on the XML file.
Sorry about that. I think Apple has “improved” iTunes in some unhelpful ways since 7.x. –ed
Might as well ask this here. I’ve been whoring this question out all over the web, and no-one, not even Apple Support has been able to help. Here goes:
I keep getting the “damaged library” message when I open iTunes. This began happening shortly after I upgraded to iTunes9 (which I don’t think is the problem, will explain later). After iTunes “reconstructs” my library file and renames my previous one “damaged,” I’m able to use iTunes for that particular session. God forbid I shut down iTunes, though. Every time I re-open iTunes, I get the damaged library error message.
Here is what I’ve done so far:
1. Called Apple “expert” support. They recommended uninstalling iTunes, quicktime, bonjour, etc and reinstalling. Not helpful.
2. Pulled out my library.itl and my library.xml files, open iTunes, and tried to reimport from these files. Worked fine… until you try to restart the computer, or open/close itunes.
3. Followed the knowledgebase articles on Apple for “recreating your library.” Again, works fine, so long as you never shut down itunes or your computer.
4. Uninstalled iTunes 9 and downgraded to iTunes 8. Same problem. (this is why I don’t think it’s an iTunes 9 problem)
5. Copied my entire library over to a 1TB external drive (thinking my HD might be nearly caput), reimported the songs… same problem.
6. Said many curse words, kicked the dog. Same problem except now my wife wants me to start going to church and the dog just growls at me.
What I’ve think might be going on: Something funky is going on when the computer or itunes is shut down. That funky something, whatever it is, is corrupting my library file.
Any ideas on how to pinpoint the problem?
I’m at my wits’ end. The next step is alcoholism!
I anxiously await some words of wisdom.
These instructions are not 100% guaranteed to work as they did in iTunes 7. Not sure what changed but people using iTunes 8 reported it wasn’t working as well. iTunes 9 is unknown at this point.
OK, here’s something you can try. In File -> Library, see what happens if you export, then shutdown cleanly. You could compare the file it creates with the one that iTunes writes on exit and experiment with swapping it into place.
Also, try moving the library w/in iTunes by changing the Media Folder location (in Preferences -> Advanced). Again, let it work, then quit iTunes and see if the file(s) look sane, as far as sizes and modification times.
Well, for the record this worked for me today (mostly) on iTunes 9 on windows – iTunes rebuilt from the XML file but then had to recheck all the audio volumes etc. as if they were new tracks – it also lost a good portion of the artwork which I had laboriously added (even for music that I had not moved) – so if this is important to you, beware. Thanks for this tip though, saved me hours!
All was perfect in my iPod world. Then my teenaged son blew up the computer, and a techie friend just reformatted it–didn’t ask first, no attempt at data recovery-like the library database files…soooo I had to download iTunes 8 on this clean old PC, but I’ve done nothing with the new iTunes install yet out of fear. The intact library (pre-vers. 8) sits on the external, but I have no matching .itl/.xml files. Then dear husband buys himself a new Nano, and starts importing CDs–so he’s begun a new library! So…do I delete *everything*, re-install iTunes 8, restore the library backup from the DVDs? I saw your instructions for rebuilding the database files, but I’m…not really understanding the binary thing and the replacing the path, etc,etc…
If you have backups, get them back in place quickly. See if iTunes can work with them. The recent additions for the nano might be gone but that’s not that important.
If you have the backups and that all works, you’re ahead of most people. Good luck.