I have 10 files in current directory:10test1test2test3test4test5test6test7test8test9testI want to remove all file except 2test and 3test, but I run command rm !(2test|3test) doesn't work. I get the following error:zsh: no matches found: !(2test|3test)
Why rm exclude using ! doesn't work in zsh?
zsh;wildcards
!(pattern) is ksh glob syntax, in zsh, you use ^(pattern) to negate the matching when extendedglob enabled:setopt extendedglobprint -rl -- ^(2test|3test)If you want to use ksh syntax, you need to enable kshglob:setopt kshglobprint -rl -- !(2test|3test)You can also use the and-not/except operator:setopt extendedglobprint -rl -- *test~[23]*(*test files except those that start with 2 or 3).Also not that unless the nobareglobqual option is enabled or you use |s within them, trailing (...) glob grouping operators conflict with glob qualifiers. For example, in !(foo) or ^(foo), the foo would be treated as a glob qualifier. You'd need ^foo or !(foo)(#q) (the (#q) adds a non-bare (explicit) glob qualifier).