How can I count how many folders (including subfolders) I have under an specific folder?
Asked
Active
Viewed 1.2k times
2 Answers
14
Use find
to select the directories and wc
to count them.
find <directory> -mindepth 1 -type d | wc -l
Dan Carley
- 25,189
- 5
- 52
- 70
-
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
-
1yes, 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
-
What if some subfoldernames have \n in their names? Wouldn't those be counted twice or more? – mxmlnkn May 18 '17 at 13:01
0
tree -d | grep directories | awk -F' ' '{print $1}'
dyasny
- 18,482
- 6
- 48
- 63
-
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