I’ve spent a lot of time poking around with my extensive iTunes database and had lots of fun figuring out different ways to address my needs with a fully digitized music collection. When a reader asked whether there was a way to just list those artists in her iTunes collection for whom she had more than one album, I thought “ah, this sounds up my alley”, and so it is.
As with all my solutions, this involves starting up by opening up the Terminal application (Applications -> Utilities) and typing the following directly on the command line or into a shell script. In this case, it’s really so easy that I suggest it might just be a nice alias.
The key to answering this problem is to recognize that iTunes stores its library in the form Artist/Album/Track, as directories, subdirectories and files. Want to see what artists you have in your library, for example? Just use:
$ cd ~/Music/iTunes/iTunes\ Music $ ls
My library is rather extensive, about 300 albums, so I’ll skip showing you this output. 🙂
Knowing that, the find command offers the solution to this question: with find you can use the little-known mindepth and maxdepth parameters to specify exactly how far into the directory tree the program should travel. To list artists and albums, but not song names, you can use the following (assuming you’ve already moved into your ‘iTunes Music’ folder):
$ find . -mindepth 2 -maxdepth 2 -print ./Al Jarreau/All I Got ./Al Jarreau/Heaven And Earth ./Alan Parsons Project/Best Of The Alan Parsons Project ./Alan Parsons Project/Eve ./Alan Parsons Project/Eye In The Sky ./Alan Parsons Project/I Robot etc, etc etc etc
That’s getting us there. Now what we need to do is actually just isolate the artist names, then count how many artist names appear more than once in the output. Make sense?
To isolate the artist names, notice that there’s a regular pattern here, of somethingslashsomethingslashsomething, which means that we can slice each line of output to get what we want by using the slash as a delimiter to the useful application cut. It’s the second field we’d like to see here (the first is just the ‘.’, the shorthand for ‘this directory’ in the output), so here’s the command needed:
$ find . -mindepth 2 -maxdepth 2 -print | cut -d/ -f2 Al Jarreau Al Jarreau Alan Parsons Project Alan Parsons Project Alan Parsons Project etc etc
All that’s left is to asceratain how often each artist name appears, and that can be done with the useful uniq (say “unique”) command’s -c flag, which does exactly what we see:
$ find . -mindepth 2 -maxdepth 2 -print | cut -d/ -f2 | uniq -c 2 Al Jarreau 9 Alan Parsons Project 2 Andreas Vollenweider 2 Bee Gees 1 Bill Whelan 1 Billie Holiday 1 Billy Joe Walker, Jr_ 6 Billy Joel 2 Bobby McFerrin 11 Bruce Hornsby 5 Bruce Springsteen etc etc
We’re soooo close now. Just two steps left: remove those that only have one occurrence (like Bill Whelan and Billie Holiday), then sort the remaining data from largest to smallest number of matches. The former can be accomplished by using a specialized version of grep that uses a regular expression (we don’t want to just screen anything out that has ‘1’ because it’ll match ’11’ too and Bruce Hornsby will vanish. Not good). To sort numerically, we’ll use sort and add the -n flag for numeric, and the -r reverse flag to sort largest to smallest. Add it all up and here’s the final output:
$ find . -mindepth 2 -maxdepth 2 -print | cut -d/ -f2 | \ uniq -c | grep -vE '( 1 )' | sort -rn 12 Ella Fitzgerald 11 Bruce Hornsby 9 Kate Bush 9 Alan Parsons Project 7 Kenny Loggins 6 Billy Joel 5 Sting 5 Bruce Springsteen 4 Paul Simon 4 Don Henley 3 The Moody Blues 3 Robert Johnson 3 Phil Collins 3 Pat Metheny Group 3 James Taylor 3 Genesis 3 Dire Straits 2 Spyro Gyra 2 Huey Lewis & The News 2 Bobby McFerrin 2 Bee Gees 2 Andreas Vollenweider 2 Al Jarreau
That’s the solution and, as I said in the beginning, it’s not really even enough to make a good shell script. Instead, just prefix the command with “alias favorites=” and surround the entire command with quotes and we’ll be able to get this information at any time by simply typing favorites on the Terminal command line.
If you’ve found this interesting, by the way, you’ll doubtless love the best-selling Wicked Cool Shell Scripts, where I show over 100 different scripts in just this manner, many specifically for Mac OS X.