
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.
Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.
Categorized:
Mac OS X Help
, Shell Script Programming
(Article 3763)
Tagged: Previous: Can I track an RSS feed with a shell script? Next: How do I scan my Windows 2000 disk to find bad blocks? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. I have a slight correction to your script. I use find "/Users/Shared/iTunes/iTunes Music" -mindepth 2 -maxdepth 2 -print | grep -v .DS_Store | \ The important changes: Actually, I didn't want to hardcode the directory because it's really better form to find out where the music library is stored by looking in the configuration XML file, which can be done thusly: itunehome="$HOME/Music/iTunes" musiclib="/$(grep '>Music Folder<' "$ituneconfig" | cut -d/ -f5- | \ With that, you would want to use the "cd" to get to that directory (even if it's ~/Music/iTunes/iTunes Music/ most of the time). In terms of filtering out .DS_Store, it's not a problem because the only time that would affect the output was if there was more than one file called ".DS_Store" in a given artist's directory (remember, we sort by artist before we calculate the number of subdirectories). Consider: $ find $iTunesHome -name ".DS_Store" -print I can't have two of them in a single artist's directory (since you can't have two files with the same name in a single directory), so there's no way that they'd survive the pipe I built. And, finally, in terms of the masking pattern to eliminate those artists with only a single album, I agree that your pattern is better than mine. Thanks for your great suggestions! Posted by: Dave Taylor at November 19, 2004 4:38 AMYou still get a count that is wrong. In your example, Bruce Springsteen, Ella Fitzgerald, Joe Jackson, Kate Bush & The Beatles will all have counts 1 too large. Do a "ls The\ Beatles" and compare the number of subdirectories with the count your script gives. This is because you aren't counting subdirectories. You are counting items in the artist directory. If you were to say find . -mindepth 2 -maxdepth 2 -type d -print Then you would be counting subdirectories. The -d flag is key. This is probably a better fix then explictly filtering out .DS_Store files. /John Posted by: John Wenn at November 20, 2004 10:49 PMOops. I meant the -type d flag is key Posted by: John Wenn at November 20, 2004 10:51 PMYou're right: Those directories that have a .DS_Store file do throw off the count on this script, but it's easily fixed, as you say, by just specifying that "find" should only match directories. Another solution could be to use a "| grep -v .DS_Store" within the pipeline, but constraining find to directories is undoubtedly the smarter solution. Thanks for the eagle eye on this! Posted by: Dave Taylor at November 21, 2004 1:20 AMOne last fiddle, I promise. The remaining thing that bothered me about the script was the sort order. It was Changing the sort to sort +0rn +1f corrects all the problems. /John Posted by: John Wenn at November 21, 2004 5:16 AMAlright, now you're getting into personal preferences in terms of how sophisticated you want things to be versus just demonstrating the basic concept, John. Maybe you're being a bit of a, um, perfectionist here? :-) Besides, you'll notice that I don't have any albums from artists who don't know what the SHIFT key is for on their keyboards! Posted by: Dave Taylor at November 21, 2004 5:36 AMSweet writeup, thanks! ((is it against the rules to go back and edit the below comments into the above text?)) Anyway, I was looking for a way to figure out how many open files each process had open--I knew lsof, but I didn't know that uniq had a -c option. Thanks for randomly having the answer I needed! I don't know how platform specific lsof's layout is, but -- lsof | tr -s ' ' ' ' | cut -d ' ' -f 2 | uniq -c | sort -n +0 (for that matter, I either didn't know or had long ago forgotten sort could take multiple fields) Posted by: kaolin fire at January 9, 2006 10:59 PMthe itunes shell command is neat but is there a way to get a command prompt to do that in windows? Posted by: william at February 11, 2007 5:19 PMI can't get that command to work. Posted by: Bruce Friedman at April 14, 2008 10:00 AMI have a lot to say, but ...
I do have a comment, now that you mention it!
|
Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.
Help!
Subscribe to
Ask Dave Taylor!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Join the List!
Book Links
|