I have a very specific directory tree on my music library. I would like to count all of the albums, excluding all the subdirectories.

For instance, given this directory tree:

/music/Davis, Miles/                           
/music/Davis, Miles/Kind of Blue/                
/music/Davis, Miles/Tutu/                    
/music/Hendrix, Jimi/                              
/music/Hendrix, Jimi/Axis Bold As Love/               
/music/Hendrix, Jimi/Electric Ladyland/              
/music/Hendrix, Jimi/Electric Ladyland/Disk 1   
/music/Hendrix, Jimi/Electric Ladyland/Disk 2

I would like the command to return "4" .

So basically I want the command to return the number of albums, or, bonus points if it returns "2" Interprets and "4" albums.

(And double bonus points if it additionally lists the interprets and albums.)

How can I most easily do this?

  • What is an "interpret"?
    – John1024
    Aug 10, 2016 at 23:35

3 Answers 3


To see the four albums:

$ ls -d1 music/*/*/
music/Davis, Miles/Kind of Blue/
music/Davis, Miles/Tutu/
music/Hendrix, Jimi/Axis Bold As Love/
music/Hendrix, Jimi/Electric Ladyland/

To count them:

$ printf "%q\n" music/*/*/ | wc -l

The %q format is used here to prevent album names with newlines from being counted more than once.

To see the artists:

$ ls -d1 music/*/
music/Davis, Miles/
music/Hendrix, Jimi/

To count the number of artists:

$ printf "%q\n" music/*/ | wc -l

The above commands assume that music is in the current directory. If the music directory is actually /music, then put a / in front of music in all of the above commands. Otherwise, put in front whatever path will reach music.


You can use find to list all the subdirectories. Then just count the number of slashes per line and report how often there were 3 of them:

find -type d | sed s%[^/]%%g | grep -c ^///$

Use ^//$ to get the number of interprets.


I think you're looking for a little 'find' magic. Running

find /music -mindepth 2 -maxdepth 2 -type d | wc -l

should give you the number of albums in your collection. Running

find /music -mindepth 1 -maxdepth 1 -type d | wc -l

should return the number of artists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.