Bash 如何删除未以“;结尾的文件;。c”;在使用shell脚本的目录中?

Bash 如何删除未以“;结尾的文件;。c”;在使用shell脚本的目录中?,bash,Bash,我就这样试过了 find -type f -name '*\.[^c]' -exec rm -f {} \; find . -type f |grep -v '.*\.c' | xargs rm -f 似乎它们不正确尝试使用否定您的姓名搜索 find -type f ! -name '*\.c' -exec rm -f {} \; 请使用delete命令尝试此操作: find -not -name "*.c" -type f -delete 字符的否定是不是模式匹配的^ -name '*.

我就这样试过了

find -type f -name '*\.[^c]' -exec rm -f {} \;
find . -type f |grep -v '.*\.c' | xargs rm -f

似乎它们不正确

尝试使用
否定您的姓名搜索

find -type f ! -name '*\.c' -exec rm -f {} \;

请使用delete命令尝试此操作:

find -not -name "*.c" -type f -delete

字符的否定是
不是模式匹配的
^

-name '*.[!c]'
但这并不正确,因为这需要任何名称以点和单个字符结尾,而不是

如果您使用的是正则表达式,则需要使用锚点($):

但是以这种方式使用“grep”和xargs将无法正确处理带有空格或特殊字符的文件名

因此,如果您试图删除当前目录及其所有子目录中的文件,请尝试:

find . -type f ! -name "*.c" -exec rm -f {} +

-not
的GNU
ism。很好!你永远不会独行:)
find . -type f ! -name "*.c" -exec rm -f {} +