count() echo $#
count * # non-hidden files (all types)
count *(D) # files (all types)
count **/*(D) # files recursively (all types)
count **/*(D/) # directories only (recursively)
zsh globs never include
.. even when
dotglob is enabled (like with the
(D) globbing qualifier)).
To avoid an error message when there is no file, add the
N globbing qualifier:
That causes the glob to expand to nothing (no argument, not an empty argument) if there's no match.
Note that since the newline character is as valid a character as any in a file name, piping the output of
wc -l is not correct.
POSIXly, you could count the
/ characters instead of the newline ones:
find . ! -name . -prune -print | grep -c /
Or recursively, using this trick:
find .//. ! -name . -prune -print | grep -c //