How to recursively delete multiple files with different extensions?

白昼怎懂夜的黑 提交于 2019-12-07 07:04:18

问题


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.

So far I got this command from another post but 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.


回答1:


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

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

OR

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



回答2:


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 -or as well. Before deleting, test it first without the -delete option. (2) Added a pair of () as suggested by gniourf_gniourf.




回答3:


Maybe regexp will help you

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



回答4:


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



来源:https://stackoverflow.com/questions/24058921/how-to-recursively-delete-multiple-files-with-different-extensions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!