This question already has an answer here:

In windows you get a count of the number of subdirectories within a directory.Is there any equivalent on Linux ? I'd like it to count recursively & Not stop at a single level.

marked as duplicate by don_crissti, Scott, dhag, cas, slm Nov 29 '15 at 3:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    @HaukeLaging That question is not the same, note "I'd like it to count recursively..." – goldilocks Nov 18 '14 at 10:19

Use find to count all directories in a tree starting from current directory:

find . -mindepth 1 -type d | wc -l

Note, that -mindepth is required to exclude current directory from the count.

You can also limit depth of search with -maxdepth option like this:

find . -mindepth 1 -maxdepth 1 -type d | wc -l

More find options are available. You can check man page for that,


If you want to get number of directories and files use this:

tree /path/to/given/dir | awk 'END{print}'

If you want only number of directories, add -d option:

tree /path/to/given/dir -d | awk 'END{print}'

tree works recursively.

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