如何使用 ZSH 从命令中排除文件?

yPh*_*hil 6 bash zsh globbing

鉴于此目录内容:

one.file
two.file
three.file
Run Code Online (Sandbox Code Playgroud)

在 bash 中,当我输入时

rm *.file !(two)
Run Code Online (Sandbox Code Playgroud)

只有one.filethree.file被删除。我怎样才能在 ZSH 中做到这一点?

小智 9

我也对这个答案很感兴趣,快速搜索了这篇关于在 zsh 中通配符的文章。亮点:

  • ^作为否定。比如ls ^two.file只会列出one.file和three.file。
  • 您可以组合^*。例如,ls ^two*将列出不以“two”开头的任何内容
  • 您可以使用括号进行更复杂的匹配。例如,ls (^two).file将列出以“two”开头以“file”结尾的任何内容。

  • 首先启用扩展通配符:`setopt extended_glob` (6认同)