Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have seen other threads about deleting a certain extension in a directory. However, when I try it, it seems to work but takes a long time to go through directories and say "Permission denied" on the system ones. All I want is a bash script that goes through the entire computer, or part of it, and deletes a certain extension. If python would be faster that is fine.

share|improve this question

migrated from Dec 21 '12 at 4:20

This question came from our site for professional and enthusiast programmers.

would it be dangerous if it is just deleting a certain extension... what could go wrong? – user1920468 Dec 21 '12 at 2:48
What do you mean by extension? Like, a Python module? – kuyan Dec 21 '12 at 2:55
What are you really trying to do? – billybob Dec 21 '12 at 3:19
I mean a file extension. – user1920468 Dec 21 '12 at 3:42

You could just use find I guess.

find / -name "*.myextension" -exec rm {} \;

But of course, searching a whole HDD (or partition..) from / is a long thing to do, and you will get permission denied errors as long as you're not running this as root.

share|improve this answer
Would python be faster? – user1920468 Dec 21 '12 at 2:47
Because it's a fork for every single rm. You should either use -exec rm -- {} +, or the GNU find -delete. – ormaaj Dec 21 '12 at 7:29
Or pipe find output into xargs rm, making suitable effort to handle filenames with whitespace – glenn jackman Dec 21 '12 at 12:08
@glennjackman May you please exactly what command to use for doing the same thing but making it work with spaces? Where would I put xargs rm? – user1920468 Dec 21 '12 at 18:10

using xargs, assuming you have GNU find and xargs:

find / -name \*.myextension -print0 | xargs -0 rm

This method uses the null char \0 as the separator between filenames: since newline is a legal character in a filename, reading find's output line-by-line is not 100% safe.

share|improve this answer
It says file: invalid option -- a file: invalid option -- t Usage: file [-bchikLNnprsvz0] [-e test] [-f namefile] [-F separator] [-m magicfiles] [-M magicfiles] file... file -C -m magicfiles Try file --help' for more information.` – user1920468 Dec 21 '12 at 20:50
I am on a Mac, could that be the problem? – user1920468 Dec 21 '12 at 20:52
No, typo, should be find not file – glenn jackman Dec 22 '12 at 3:55
I'm sorry to trouble you, but I named a file "with spaces.myextension" and put it on the desktop. I then did the command find /users/<myusername>/Desktop -name \*.myextension -print0 | xargs -0 rm and the file remained there. – user1920468 Dec 22 '12 at 15:32
Do you see it with find /users/<myusername>/Desktop -name \*.myextension -print ? – glenn jackman Dec 22 '12 at 22:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.