Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I count how many folders (including subfolders) I have under an specific folder?

share|improve this question
up vote 14 down vote accepted

Use find to select the directories and wc to count them.

find <directory> -mindepth 1 -type d | wc -l
share|improve this answer
Wow, is there an faster solution? ;-) ------------------------------------------------- time find product_images/ -mindepth 1 -type d | wc -l 574292 real 67m34.672s user 0m4.272s sys 0m35.790s ------------------------------------------------- – Max Jul 20 '09 at 10:56
That's a big tree. The only way you can speed it up is to work on an index of that structure. But creating and updating the index will be just as slow. And the most common tool for indexing and searching the local FS, locatedb, won't allow you to refine your search in the same way as find. – Dan Carley Jul 20 '09 at 12:33
yes, with 574292 subdirs and who knows what many files, it's the fastest way to cound the dirs – asdmin Jul 20 '09 at 19:41

tree -d | grep directories | awk -F' ' '{print $1}'

share|improve this answer
I'd use tail since there could be a directory called "directories" and there's no need for the -F with the awk: tree -d|tail -n1|awk '{print $1}' – Dennis Williamson Jul 20 '09 at 13:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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