8

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.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Dhiwakar Ravikumar
  • 311
  • 2
  • 6
  • 12

2 Answers2

9

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,

rush
  • 27,055
  • 7
  • 87
  • 112
1

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.

αғsнιη
  • 40,939
  • 15
  • 71
  • 114