bash 如何递归删除具有不同扩展名的多个文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24058921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:37:34  来源:igfitidea点击:

How to recursively delete multiple files with different extensions?

linuxbashfinddelete-file

提问by Fiztban

I am trying to write a command to remove recursively several files with different extensions (*.extension1, *.extension2 etc) from the current directory and all its related sub-directories.

我正在尝试编写一个命令,以从当前目录及其所有相关子目录中递归删除多个具有不同扩展名(*.extension1、*.extension2 等)的文件。

So far I got this command from another postbut I couldn't workout how to adapt it to more than one file in the same command line:

到目前为止,我从另一篇文章中得到了这个命令,但我无法锻炼如何在同一命令行中将其调整为多个文件:

find . -name "*.extension1" -type f -delete

Is it as simple as below?

是不是像下面这么简单?

find . -name "*.extension1";"*.extension2" -type f -delete

Just as a side note, these are all output files that I do not need, but not all are necessarily always output so some of the extensions might not be present. This is just as a general clean-up command.

顺便提一下,这些都是我不需要的输出文件,但并非所有文件都必须始终输出,因此某些扩展名可能不存在。这就像一般的清理命令一样。

回答by Jayesh Bhoi

find . \( -name "*.extension1" -o -name "*.extension2" \) -type f -delete

find Documents ( -name ".py" -o -name ".html" ) -exec file {} \;

查找文档 ( -name " .py" -o -name ".html" ) -exec file {} \;

OR

或者

find . -regextype posix-egrep -regex ".*\.(extension1|extension2)$" -type f -delete

回答by konsolebox

Just add more options. A regex solution can also apply but this one's better and safer.

只需添加更多选项。正则表达式解决方案也可以应用,但这个更好更安全。

find . \( -name '*.ext1' -or -name '*.ext2' \) -type f -delete

Edit: You probably need -oras well. Before deleting, test it first without the -deleteoption. (2) Added a pair of ()as suggested by gniourf_gniourf.

编辑:您可能也需要-or。在删除之前,请先在没有-delete选项的情况下进行测试。(2)()根据 gniourf_gniourf 的建议添加了一对。

回答by nervosol

Maybe regexp will help you

也许正则表达式会帮助你

find . -regextype posix-awk -regex "(.*.ext1|.*.ext2)" -type f -delete

回答by ThisIsTrinadh

This simple command will delete all the files with extension1 and extension2 recursively in that directory. rm find . -name *.extension1 -o -name *.extentions2

这个简单的命令将递归删除该目录中带有 extension1 和 extension2 的所有文件。R Mfind . -name *.extension1 -o -name *.extentions2