Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

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*

share|improve this answer
    
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? –  user815423426 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
1  
@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
add comment

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

print -l 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.

share|improve this answer
add comment

Your Answer

 
discard

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.