Match all files except specific ones in zsh
You can use the ^(pattern1|pattern2|...)
syntax to match everything except the given patterns in zsh.
First you have to enable extended globbing, using setopt extended_glob
. For example:
touch foo; touch bar; touch baz
setopt extended_glob # this is needed for the pattern to work
echo ^(foo|bar) # outputs "baz"
The case with a single name is a little bit counterintuitive, because the “ | ” symbol is mandatory: |
echo ^(bar|) # outputs "baz foo"
You can complicate the pattern further:
echo ^(ba*|) # outputs "foo"