Fiztban
Fiztban

Reputation: 275

How to recursively delete multiple files with different extensions?

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.

Upvotes: 6

Views: 10816

Answers (5)

Alec Rust
Alec Rust

Reputation: 11305

Another solution using rm:

rm -rf ./**/*.{txt,nfo,jpg,jpeg,png,exe,url}

If you want to delete other files too for e.g. those starting with sample. just add that too:

rm -rf ./**/*.{txt,nfo,jpg,jpeg,png,exe,url} ./**/*/sample.*

Upvotes: 1

ThisIsTrinadh
ThisIsTrinadh

Reputation: 1

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

Upvotes: 0

konsolebox
konsolebox

Reputation: 75458

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.

Upvotes: 2

Jayesh Bhoi
Jayesh Bhoi

Reputation: 25865

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

Upvotes: 15

nervosol
nervosol

Reputation: 1291

Maybe regexp will help you

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

Upvotes: 1

Related Questions