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?

  • 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 -- **/*? Apr 10, 2013 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). Apr 10, 2013 at 8:51

2 Answers 2


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/)#


  • 5
    This is a great answer — though it requires setopt EXTENDED_GLOB. Without it, you'll get zsh: bad pattern: [...]. Apr 3, 2016 at 21:39
  • I'm having trouble applying your answer to my use case. I'm trying to match the set of CMakeLists.txt files scattered in various subdirectory, excluding all directories starting with cmake-* from being searched. I tried (^cmake-*)**/CMakeLists.txt#; it works, but it doesn't include the CMakeLists.txt in my working directory (i.e. it finds those located in subdirectories exclusively). How can I include it? Thanks, and great answer!
    – brainplot
    Apr 4, 2019 at 23:27
  • @brainplot If you want to exclude cmake-* at any level, then like in my answer, use (^cmake-*/)#CMakeLists.txt. If you only want to exclude cmake-foo/bar/CMakeLists.txt but not baz/cmake-qux/CMakeLists.txt, you need a different approach: **/CMakeLists.txt~cmake-*/*. Apr 4, 2019 at 23:35
  • @Gilles all the cmake-* folders I wanted to exclude are in my current directory and I wanted to exclude them all. I tried your first solution and it worked. Thank you!
    – brainplot
    Apr 4, 2019 at 23:49

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' **/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .