17

Say I have the following files:

|-- bar
`-- foo
    |-- type_A_1
    |-- type_A_2
    |-- type_B_1
    |-- type_B_2
    |-- type_B_xx
    |-- type_B_xx
    `-- something_else

I thought the following command

print -l foo/*~{type_B*}

would print everything under foo except things that start with type_B but it doesn't, instead it prints everything under foo:

foo/type_A_1
foo/type_A_2
foo/type_B_1
foo/type_B_2
foo/type_B_xx
foo/something_else

I also tried print -l foo/*~type_B and got the same thing.

How does the exception wildcard ~ work in zsh?

9

You need to include the directory into the exception: print -l foo/*~foo/type_A* or print -l foo/*~{foo/type_A*}.

If you want, you can replace the directory by a wildcard: print -l foo/*~*/type_A*

  • Thanks! About the second pattern, do you mind explaining how the pattern foo/*~*/type_A* works? Does the second * expand to every folder under the working directory, or is it smart enough to only expand to foo? – Amelio Vazquez-Reina Feb 22 '12 at 16:26
  • @roseck Because it seems really fast (for example when you run print *~**/* in the / directory), I assume that it first expand the first pattern and then remove all matches which fit to the exclusion pattern. – jofel Feb 22 '12 at 16:36
  • 2
    @roseck ~ is purely textual, the part before the ~ expands as it always would, then the part after strips away matches. ​@jofel A better test would be print /**/*~*: you can observe that it traverses the whole tree, even if it ends up printing nothing. – Gilles Feb 22 '12 at 23:43
  • I thought he wanted to exclude type_B but include type_A. – systemovich Dec 8 '17 at 18:08
  • Right answer is below – aryndin Jan 28 at 13:21
28

zsh has the ^ glob operator when EXTENDED_GLOB is on. It seems like the perfect fit for your stated situation:

setopt extendedglob
print -rl foo/^type_A*

It means “match anything, except what matches the following pattern”, but its effect is limited to the portion of the pattern between slashes, or between the beginning of the pattern and the first slash, or (as in this case) between the last slash and the end of the pattern.

You can view the zsh options currently enabled with:

setopt

And disable EXTENDED_GLOB with:

unsetopt extendedglob

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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