How to count subdirectories in a directory in UNIX

directory

I have to count the total number of directories that are in the given directory (note that these are subdirectories).

I know how to count for files but am having trouble counting the directories for my script.

Best Answer

In a slightly robuster variation of @rahul's comment:

find . -type d -mindepth 1 -printf '1'  | wc -c

will print '1' for each directory in the current one that is not the current one (-mindepth 1), and then we count the 1s. This will cope with directory names that contain newlines.

Related Question