Can I list my iTunes Library by number of albums?
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 $ lsMy 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 etcThat'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 etcAlmost there.
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 etcWe'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 JarreauThat'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.
Related Mac OS X Help articles:
✔ Audacity can't find LAME library, I can't save Mp3?
Hey Dave. I read your article Audacity can't save mp3 audio files and am still puzzled because I downloaded the LAME Mp3 converter...✔ How to remove Dashboard as a "space" in Mac OS X Spaces?
I'm a big fan of the Spaces utility in Mac OS X that lets me have multiple virtual screens [see Set Up Mac...✔ Best place to buy a cheap MacBook laptop?
Hi Dave. I am looking for two gently used MacBook laptops for my teen daughters. Personal computers would greatly facilitate their studies as...✔ File too big error copying to USB flash drive on my Mac?
I'm baffled. I have a 16GB Kingston USB flash drive that I use on my Mac system and I'm trying to copy a...✔ Stealth image capture photo from webcam on my Mac?
Someone sneaks into my cubicle while I'm at lunch and takes candy out of my desk. Petty, but stupid too. I want to...
Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
I do have a comment, now that you mention it!
Check This Out Too...
Look for Answers
All Our Categories
Apple iPad Help
Articles and Reviews
Auctions and Online Shopping
Blogs and Blogging
Building Web Site Traffic
Business and Management
Computer and Internet Basics
d) None of the Above
Google Gmail Help
Google Plus Help
Industry News and Trade Shows
iPhone and Cell Phone Help
iPod, Sony PSP and MP3 Player Help
Kindle Fire Help
Mac OS X Help
Pay Per Click (PPC) Advertising
Search Engine Optimization (SEO)
Shell Script Programming
Tech Support Video Help
The Writing Business
Twitter, LinkedIn and Social Network Help
Unix and Linux Help
Video Game Tips and Help
Windows PC Help
Find Me on Google+
ADT on G+