Deleting Certain Extension with Bash (quickly)

3

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.

user1920468

Posted 2012-12-21T02:42:48.627

Reputation: 41

1would it be dangerous if it is just deleting a certain extension... what could go wrong? – None – 2012-12-21T02:48:24.230

What do you mean by extension? Like, a Python module? – Nathan – 2012-12-21T02:55:52.470

1What are you really trying to do? – None – 2012-12-21T03:19:10.837

I mean a file extension. – None – 2012-12-21T03:42:00.787

Answers

2

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.

cmc

Posted 2012-12-21T02:42:48.627

Reputation: 129

Would python be faster? – None – 2012-12-21T02:47:42.637

1Because it's a fork for every single rm. You should either use -exec rm -- {} +, or the GNU find -delete. – ormaaj – 2012-12-21T07:29:34.910

Or pipe find output into xargs rm, making suitable effort to handle filenames with whitespace – glenn jackman – 2012-12-21T12:08:09.317

@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 – 2012-12-21T18:10:01.273

1

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.

glenn jackman

Posted 2012-12-21T02:42:48.627

Reputation: 18 546

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 Tryfile --help' for more information.` – user1920468 – 2012-12-21T20:50:10.223

I am on a Mac, could that be the problem? – user1920468 – 2012-12-21T20:52:23.790

No, typo, should be find not file – glenn jackman – 2012-12-22T03:55:30.327

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 – 2012-12-22T15:32:04.770

Do you see it with find /users/<myusername>/Desktop -name \*.myextension -print ? – glenn jackman – 2012-12-22T22:48:10.300

Nothing happens. – user1920468 – 2012-12-25T15:23:44.637

I can only conclude that you have no files with that extension, spaces or not. – glenn jackman – 2012-12-25T17:02:37.860