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

I'm running zsh on Linux under setopt extended_glob ksh_glob glob_dots. I'm looking for something easy to type on the command line, with no portability requirements. I'm looking at a source code tree, with no “weird” file names (e.g. no \ in file names, no file name beginning with -).

Either of the following commands print the list of subdirectories of the current directory recursively:

find -type d
print -l **/*/

This is actually an svn checkout:

$ find -type d

I want to exclude the .svn directories and their subdirectories which are present in every directory. It's easy with find:

find -type d -name .svn -prune -o -print

Can I do this with a short zsh glob? Ignoring dot files comes close (I need to do it explicitly because I have glob_dots set):

print -l **/*(/^D)

But this isn't satisfactory because it hides the .deps directory, which I do want to see. I can filter out the paths containing .svn:

print -l **/*~(*/|).svn(|/*)(/)

But that's barely shorter than find (so what am I using zsh for?). I can shorten it to print -l **/*~*.svn*(/), but that also filters out directories called hello.svn. Furthermore, zsh traverses the .svn directories, which is a bit slow on NFS or Cygwin.

Is there a convenient (as in easy to type) way to exclude a specific directory name (or even better: an arbitrary pattern) in a recursive glob?

share|improve this question
Why would you spend some time finding out whether your files have or have not dashes or backslashes (and let other readers find out by themselves when they can't guarantee that) instead of just write the bulletproof right thing like printf '%s\n' **/* or print -rl -- **/*? – Stéphane Chazelas Apr 10 '13 at 6:59
@StephaneChazelas In a script, sure. But on the command line, saving 4 characters is worth it. In my real-world situation, I didn't need to find out about file names, I already knew (build trees tend to have tame file names). – Gilles Apr 10 '13 at 8:51
up vote 16 down vote accepted

Zsh's extended glob operators support matching over / (unlike ksh's, even in zsh's implementation). Zsh's **/ is a shortcut for (*/)# (*/ repeated 0 or more times). So all I need to do is replace that * by ^.svn (anything but .svn).

print -l (^.svn/)#


share|improve this answer
This is a great answer — though it requires setopt EXTENDED_GLOB. Without it, you'll get zsh: bad pattern: [...]. – Andy Fowler Apr 3 at 21:39

While ksh93 globbing is nowhere near zsh's even with the globstar option, in ksh93, it can be achieved with:

set -o globstar
printf '%s\n' **/
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.