I need to organise an external HDD such that there is no more than 500 folders on it.

Ubuntu's "Properties" pane shows only the file count, not the folder count.

Is there a simple CLI line that will tell me the number of subdirectories?

Thanks!

EDIT: I need to count recursively, and the drive is an external HDD mounted at /media/MUSIC/. It's for a Car Stereo who's doc says it only reads the first 500 folders.

link|improve this question

feedback

3 Answers

up vote 10 down vote accepted

Find all folders in total, including subdirectories?

find /mount/point -type d | wc -l

...or find all folders in the root directory (not including subdirectories)?

find /mount/point -maxdepth 1 -type d | wc -l
link|improve this answer
That first one is exactly what I'm after. Thanks! – Dean Apr 9 '10 at 6:07
@Dean: yeah, i've used it for counting directories in my music collection before too. :) for braggin' rights, count your MP3s: find /mount/point -type f | grep -i mp3 | wc -l – quack quixote Apr 9 '10 at 6:17
1  
@Dean: yeah, i don't know the find syntax offhand, but i'd usually use grep -v for that: find /mount/pt -type f | grep -vi mp3 | grep -vi wav | wc -l ...(and <i></i> doesn't work in comments, use *foo* to italicize: foo ) – quack quixote Apr 9 '10 at 6:25
1  
@Dean: i think you'd just use find /path -not -iname '*.mp3' | wc -l to filter MP3s, or to get both MP3s and WAVs use find /path -not -iname '*.mp3' -not -iname '*.wav' | wc -l – quack quixote Apr 9 '10 at 6:28
1  
Needed to add the -type f in order for it to ignore folders as well, but congrats! I just found a whole bunch of m4a files I had no idea about... Thanks! – Dean Apr 9 '10 at 6:53
show 1 more comment
feedback

Try the following [see below]:

ls -1 -p | grep "/" | wc -l

This will print a one-column list of the current directory, with trailing slashes for items that are subdirectories, then count the lines with the slashes.

EDIT: you should probably go with quack quixote's answer, as it is a little more explicit, but I've corrected mine (after taking quack's suggestions into account).

ls -Rp | grep "/$" | wc -l
link|improve this answer
Need to add -R to go recursive (sorry for failing to mention), and need to specify the path to ls, but yeah, this works: ls -1 -p -R /media/MUSIC/ | grep "/" | wc -l Thanks! – Dean Apr 9 '10 at 6:05
After trying both this and quack quixote's suggestion, I got 2 different results... I'm inclined to believe the other one. Thanks anyway! – Dean Apr 9 '10 at 6:07
you don't need the -1 since ls will detect the pipe and won't format columns. also you can grab recursive listings with -R, but then you probably want to grep for "/$" to only match trailing slashes (or the count will be off). final version: ls -Rp | grep "/$" | wc -l – quack quixote Apr 9 '10 at 6:10
the find version i posted will count . (ie /mount/point) in its count, the ls version in my above comment won't. those counts differ by 1, tho you could use /mount/point/* in the find command to correct that (assuming no hidden dot-directories). – quack quixote Apr 9 '10 at 6:13
i'm not sure why you got the two differnent results, but @quack's method will include "hidden" directories (ie. those that begin with '.'). – stuntmouse Apr 9 '10 at 8:45
show 3 more comments
feedback

New lines are valid characters in directory names. I suggest letting find print an empty line for each directory found and then letting wc count those lines.

find /mount/point -type d -printf '\n' | wc -l
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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