rm !(文件名)在 ubuntu 中不起作用,它显示错误“bash: !: event not found”

ske*_*esh 6 bash command

我试图删除目录中的所有文件,但在该目录中保留两个文件。

我使用了命令

rm !(1file name| 2filename) 
Run Code Online (Sandbox Code Playgroud)

但它抛出错误

bash: !: event not found
Run Code Online (Sandbox Code Playgroud)

请帮我更正命令。

anu*_*ava 5

!试图扩大历史事件。在 BASH 中,您可以启用extglob以下功能:

shopt -s extglob
Run Code Online (Sandbox Code Playgroud)

然后使用此rm命令删除除这两个列出的文件之外的所有文件:

rm !(@(filename1|filename2))
Run Code Online (Sandbox Code Playgroud)