Blog Archives

Count all folders recursively

   ls -lR | grep ^d | wc -l

ls -lR gets a long format of all files and directories of a given folder. Every line which contains a directory starts with d as the first character. So grep ^d filters all such lines (^ is for Beginning-Of-Line). wc -l counts the Newline characters in a given file or stdin. Because every line contains a single directory entry and every line ends with a newline character, you will get the number of the directories.