6

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.

5 Answers 5

15
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
Sign up to request clarification or add additional context in comments.

9 Comments

Hello @BroSlow, I am not sure what exactly you are asking me. The first suggestion didn't work. The second one did.
@Fiztban i think BrowSlow asked me.
Sorry I just realised some files come out with extensions that are not predictable but before the extension is, example mpd2.xxxxxxxxx where xxx is a randomly generated output. Could you include a way to delete those or is it easier to have it as a second command?
@Fiztban So you say that extensions should be extensionsxxx right? and here xxx will any value.
@Fiztban if i understand correctly then your output files like *.check *.err and sometimes you got like mpd2.xxx_ext but here _ext is fixed and xxx have random value. am i right?
|
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.

6 Comments

Thank you for your response. The first iteration didn't work, trying again post edit.
Nope unfortunately it isn't working still. I also tried with "*.ext1"
@Fiztban Your find probably is not GNU based or is an older version. You can try other suggestions.
find . \(-name '*.ext1' -o -name '*.ext2' \) -type f -delete otherwise -type f -delete only applies to -name '*.ext2'. Note that -or is not POSIX. Use -o instead.
@gniourf_gniourf I see. Good suggestion. I actually tried it getting same output but never thought that it actually has a difference. Or should I say I forgot.
|
2

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.*

Comments

1

Maybe regexp will help you

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.