如何删除没有特定扩展名的文件?

时间:2017-01-30 12:02:19

标签: linux shell

如何在当前工作目录中递归删除没有.txt.exe扩展名的所有文件?我需要一个单行。

我试过了:

find . ! -name "*.txt" "*.exe" -exec rm -r {} \
find -type f -regextype posix-extended -iregex '.*\.(txt|exe)$'

3 个答案:

答案 0 :(得分:4)

试试这个。

find . -type f ! -name "*.exe" ! -name "*.txt" -exec rm {} \;

上面的命令将以递归方式删除当前目录和子目录中除.exe和.txt扩展名文件以外的所有文件。

答案 1 :(得分:2)

如果你有find动作的GNU -delete

find . -type f ! \( -name '*.txt' -o -name '*.exe' \) -delete

如果没有:

find . -type f ! \( -name '*.txt' -o -name '*.exe' \) -exec rm -f {} +

使用-exec ... {} +尽可能少地执行rm,并将参数链接起来。

答案 2 :(得分:0)

rm -rf $(find . -type f ! -name "*.exe")
相关问题