I would like to print the number of folders (recursive, excluding hidden folders) in a given CWD / current directory. What command, or series of commands can I use to ascertain this information?
6 Answers
This will find the number of non-hidden directories in the current working directory:
ls -l | grep "^d" | wc -l
EDIT:
To make this recursive, use the -R
option to ls -l
:
ls -lR | grep "^d" | wc -l

- 8,595
-
Thanks. I need this to be recursive; is there a modification that would comply? EDIT: this doesn't seem to omit hidden dirs – jml Nov 11 '14 at 01:48
-
If it is not omitting hidden directories, do you have an
alias
set forls
that is listing hidden directories by default? – Timothy Martin Nov 11 '14 at 02:05 -
Good point. I had one in my
bash_profile
. Thanks for the recursion update as well. – jml Nov 11 '14 at 02:10 -
Be cautious with the -R flag for ls. Check the output without the
wc -l
pipe, because I think ls -lR adds blank lines and dir names between files, which might throw off your count. – transistor1 Nov 11 '14 at 18:36 -
3@transistor1 Good point. In this case however the
grep "^d"
displays only the entries which have ad
at the beginning of the line. Blank/non-directory lines should not be displayed without thewc -l
or counted with thewc -l
. – Timothy Martin Nov 11 '14 at 18:47 -
In the GNU land:
find . -mindepth 1 -maxdepth 1 -type d -printf . | wc -c
elsewhere
find . -type d ! -name . -printf . -prune | wc -c
In bash:
shopt -s dotglob
count=0
for dir in *; do
test -d "$dir" || continue
test . = "$dir" && continue
test .. = "$dir" && continue
((count++))
done
echo $count

- 90,279
-
-printf
is GNU specific, while-mindepth
/-maxdepth
are now found in a few other implementations. – Stéphane Chazelas Oct 31 '18 at 20:40
echo $(($(find -type d | wc -l) - 1))
is one way (subtract 1 from the wc -l to remove the current dir). You can tweak the options to find to find different things.
echo $(($(find -type d -not -path '*/\.*' | wc -l) - 1))
- to exclude the hidden dirs
As I mentioned in the comments, the heart of this expression is really find -type d
, which finds all directories.
Note this finds all subfolders as well - you can control the depth using the -maxdepth
flag.

- 297
-
that's interesting - so, there's no built in command to do something like this? – jml Nov 11 '14 at 01:16
-
1@jml - Someone else likely has something simpler, but
find
is my go-to for file finding & counting. The "meat" of this ugly expression is really justfind -type d
, which finds any directories. – transistor1 Nov 11 '14 at 01:22
In zsh
:
(){echo $#} *(N/)
Recursively:
(){echo $#} **/*(N/)
Add the D
glob qualifiers if you also want to count hidden directories.
POSIX equivalents:
ls -p | grep -c /
(add the -A
option to ls
for hidden ones)
Recursively:
LC_ALL=C find .//. ! -name . \( -name '.*' -prune -o -type d -print \) |
grep -c //
Or
LC_ALL=C ls -Rqn . | grep -c '^d'
To include hidden ones:
LC_ALL=C find .//. ! -name . -type d | grep -c //
or:
LC_ALL=C ls -ARqn . | grep -c '^d'

- 544,893
find . -type d -not -path '.' -printf 0 | wc -c ;
Recursively find all directories (-type d) within current directory (find .) that is not (.) directory and print 0 for each directory found. Then wc -c
counts the number of characters (0) from the previous command output.

- 191
-
2This is essentially the same as the first part of https://unix.stackexchange.com/a/167245/116858 – Kusalananda Oct 31 '18 at 20:28
wc
won't work because it counts files. wouldls
work with a for loop in a bash script or is there something else I'm missing? – jml Nov 11 '14 at 01:03