Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there any way to prevent find and ls -l from listing . and .. in their results? I never care about seeing this result in the output, and it prevents me from effectively piping their output to wc -l to count things accurately.

In case it matters, I am working in zsh.

share|improve this question
You must have ls aliased to ls -a or something similar; ls does not include . or .. by default. – chepner Oct 17 '13 at 16:52
up vote 4 down vote accepted

With zsh:

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 . nor .. 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:

count *(ND)

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 ls or find to 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 //
share|improve this answer
This is great. One thing I noticed though is that if a folder has 0 files count folder outputs no matches found. Is there any way to make it return 0? – Josh Oct 8 '13 at 18:13
@Josh, yes, use the N globbing qualifier (nullglob) – Stéphane Chazelas Oct 8 '13 at 19:28

For ls, use -A instead of -a.

from man ls:

   -A, --almost-all
          do not list implied . and ..
share|improve this answer

To get find to exclude the dot directories you can use this:

$ find . ! -path . -type d


$ find . ! -path . -type d | head -5
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.