Clearing orphaned music files after iTunes library migration

When migrating your iTunes library from one computer to another, you may find that all the songs have been duplicated in their storage locations on the hard drive and have been given a naming pattern to differentiate it from its corresponding duplicate. For most cases of file duplication an easy approach may be easy to just remove the duplicated file; however, in the case of iTunes libraries there can be thousands of files in multiple directories.

When migrating your iTunes library from one computer to another, you may find that all the songs have been duplicated in their storage locations on the hard drive and have been given a naming pattern to differentiate it from its corresponding duplicate. For most cases of file duplication an easy approach may be easy to just remove the duplicated file; however, in the case of iTunes libraries there can be thousands of files in multiple directories.

Recently we were contacted by a MacFixIt reader "Rachel" who ran into this issue after migrating to a new computer.

"I transferred all my music over to the new computer, I see now that in each subfolder where the song is listed, it is listed twice! Once as the actual song and the second as the song with the number 1 at the end. For example: "Rich Girl.mp3" and "Rich Girl 1.mp3" -- is this using up A LOT of extra space? Or is there a simple way to consolidate these songs, or is the only way to go through each folder manually and just delete the song with the "1" at the end of the name??"

The problem was trying to easily locate all the duplicate files on the drive that were not being used by the iTunes library, and remove them. The initial approach seemed simple enough, which was to do a Finder search for all files with "1.mp3" in their names, and then delete them; however, it turned out the newly imported files containing "1" in their names were the ones being used in the iTunes library. Therefore, not only were there thousands of files, but the easy way to locate the duplicates (searching for the common text in the name) would not do any good since removing them would break the file paths in the library.

This meant the solutions available were the following:

  1. Leave it be

    Having the duplicate files on the drive will not hurt anything; however, they will take up space that could be used for other things. This was not the desired option and the point was to free up the space used by the duplicate songs.

  2. Manually delete the files

    This is the most basic way of managing the problem, which is to open a Finder window and go through each folder to remove the duplicate files one-by-one. The problem here is it's tedious and you may inadvertently miss some folders containing duplicates. Nevertheless, when using "Column" or even List views you can speed things up by using the arrow keys to go into folders and check them for duplicates.

  3. Remove all files and re-import them to iTunes

    Another option is to clear out your iTunes library, remove the files and folders in the iTunes Music folder (or /"iTunes Media"/Music/ folder) and re-import your files; however, this may result the loss of metadata such as play count, ratings, and other information.

While these options seem to be the only available ones for managing this problem, there are a couple of other possibilities that may work as well. These include using an iPod to reset the entire library, and using Automator to target the files for deletion.

Using an iPod

If you have an iPod that is large enough to accommodate all the files in your music library, you can use it to save your library, then reset it on iTunes and copy it back. This will allow you to save the library's metadata that otherwise would be lost upon reimporting your music.

To do this, follow the instructions in this Apple knowledgebase document to set up the iPod to contain the whole iTunes library. Be sure you do not have the option to downsample songs to 128Kbps enabled, and also that you do not have the program set to synchronize only the checked songs (both of these are available in the "Summary" window).

iTunes' option to keep files being removed
Be sure to click "Move to Trash" so the deleted files are removed from your computer (click for larger view).

Before transfering the music back as is mentioned in the knowledgebase document, be sure to disconnect the iPod and clear your iTunes library by selecting all files that were synchronized and delete them. When prompted, select the option to move the deleted file to the trash. After this is done, go to the iTunes Media (or iTunes Music) folder and remove all the album and artist folders still present. At this point these should just contain the duplicate files and others that were not in your iTunes library.

Using Automator and Finder Search capabilities

Apple's "Automator" can be used to locate and target all the files not included in the iTunes library so you can delete them. Unfortunately this is not as straightforward as it could be because like to the Finder, Automator does not have the ability to find or filter items in the Finder by exclusion (i.e., including all except a specific search criteria). If Apple had this filtering option available, finding the unused duplicate files in the Finder would have been as easy as searching for items that do not include "1.mp3," but this is not an option.

Despite these limitations, you can get around them by using the Finder's labels for files and folders. The idea is to set up Automator (or the Finder) to first search for all files that do include "1.mp3" in their names (those that the iTunes Library is using), and then label all the found items with a unique color in the Finder. Once this is done it will be easy to do a Finder search on the folder for files that are not labeled, and be able to remove them in batches.

To do this, create a new workflow and then put the following workflow actions in this order:

Automator Workflow to label duplicate files
Use this workflow to label the duplicated files that are currently being used by iTunes (click for larger view).
  1. Get Specified Finder Items

    Be sure to add the iTunes Music folder (or the "Music" folder within the iTunes Media folder) to the list. You can also use the "Ask for Finder Items" workflow action, but it requires you to select the iTunes Media/Music folder each time the workflow is run. The goal here is to somehow target the parent folder that contains the folders for your artists and albums.

  2. Get Folder Contents

    Be sure to check the option to "Repeat for each subfolder found"

  3. Label Finder Items

    Add this workflow and click the little "x" to ensure all found items are stripped of their labels before the workflow moves on.

  4. Filter Finder Items

    In the first one of these filtering workflow actions, change it so the kind of file is "Music". This will ensure only music files are passed to the second "Filter Finder Items" action, and allowing the "Any" condition in the next step to work on this subset of files instead of all the ones in the targeted directory.

  5. Filter Finder Items (again)

    This is the key action to modify. With the criteria set so "Any" of the conditions should be met, add as many options to filter by name as you would like to include different file types in your library. Keep in mind that adding more items here will include more files in the search results; however, you can always narrow the resulting list by adding an additional "Filter Finder Items" workflow action after this one. Having multiple filters allows for you fine-tune the search parameters.

  6. Choose from List

    Add this workflow action in here as a precautionary step, which will list all the files being targeted so far. At this point you can choose to exclude any files you want to keep by unchecking the corresponding box.

  7. Label Finder Items

    Select any color to use, but keep in mind this whole procedure assumes the files you wish to delete will not be included by the search filters in the 5th an 6th actions in this workflow, and therefore will no be labeled by this step.

When run, this workflow will go through the music in your folder, and label all that you want to keep (based on the filter criteria you set up) with the selected label color. Keep in mind you can do something similar by using a Finder search like the procedure described below, so that is an option as well. The goal is to somehow target the duplicated files (that are being used by iTunes) that all share a similar naming pattern, and give them the same label so you can use the label to exclude these files later on.

Finder Search of Unlabeled Music
A Finder search of the folder (in this case it's my "iTunes Music" folder) for music files with no label should show the remaining files that are unused by iTunes (click for larger view).

Once the files have been labeled, you can easily find the remaining ones by using a Finder search to target the files without any labels. To do this, go to the same iTunes Music folder you targeted with the workflow, and press Command-F to bring up the Find window. From here modify the search parameters right under the window's toolbar so the computer searches within "iTunes Music" (or the "Music" folder within the "iTunes Media" directory, if you have your music organized that way), and then be sure to search by File Name instead of Contents.

Now modify the filters so the search includes only music files with no file label. To do this, next to the filter menu entitled "Kind" change "Any" to "Music," and then add a new search filter by clicking the plus button to the right side of the current search filter. In the new search filter, choose "Other" from the first menu, and locate the filter called "File label" (you may need to use the list's search box to find it). Select it, click OK, and then be sure to click the "x" in the label options for this filter so the search only includes items with no label.

With these search parameters set up, you should be able to see all the duplicate songs in the search results, and be able to move them or delete them as you see necessary.



Questions? Comments? Have a fix? Post them below or email us!
Be sure to check us out on Twitter and the CNET Mac forums.

About the author

    Topher, an avid Mac user for the past 15 years, has been a contributing author to MacFixIt since the spring of 2008. One of his passions is troubleshooting Mac problems and making the best use of Macs and Apple hardware at home and in the workplace.

     

    ARTICLE DISCUSSION

    Conversation powered by Livefyre

    Don't Miss
    Hot Products
    Trending on CNET

    Hot on CNET

    Saving your life at speed and in style

    Volvo have been responsible for some of the greatest advancements in car safety. We list off the top ways they've kept you safe today, even if you don't drive one.