Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How do I safely delete all files with a specific extension (e.g. .bak) from current directory and all subfolders using one command-line? Simply, I'm afraid to use rm since I used it wrong once and now I need advice.

share|improve this question

6 Answers 6

up vote 124 down vote accepted

You don't even need to use rm in this case if you are afraid. Use find:

find . -name "*.bak" -type f -delete

But use it with precaution. Run first:

find . -name "*.bak" -type f

to see exactly which files you will remove.

See man find and man rm for more info and see also this related question on SE:

share|improve this answer
How's this different from rm *.bak? –  Bolt64 Nov 15 '13 at 13:11
@Bolt64 Your rm *.bak will not work for subdirectories. –  Radu Rădeanu Nov 15 '13 at 13:14
With default settings rm *.bak will only delete all files ending with .bak in the current directory. TO also do things in subdirectories you either needed to fiddle with globs, use the -r option or use the find example. –  Hennes Nov 15 '13 at 13:14
@Hennes Be careful with rm -r *.bak! It also removes directories ending in .bak with all their content. –  Radu Rădeanu Nov 15 '13 at 13:34
Make sure that -delete is the last argument in your command. If you put it before the -name *.bak argument, it will delete everything. –  Michael Oct 29 '14 at 14:36

Deleting files is for me not something you should use rm for. Here is an alternative:

find . -name "*.bak" -type f  # display what will be deleted
sudo apt-get install gvfs     # install a tool that allows you to put stuff in the trash
alias "trash"="gvfs-trash"    # you can also put this in .bash_aliases or simply use the command without alias
trash *.bak                   # trash the files (thus moving them to the trash bin)

As Flimm states in the comments:

The package trash-cli does the same thing as gvfs-trash without the dependency on gvfs.

share|improve this answer
"Don't use rm to delete things" is a controversial statement but I have to agree that it's often wiser to use something that will let you undo things. –  Oli Nov 15 '13 at 15:12
The package trash-cli does the same thing as gvfs-trash without the dependency on gvfs. –  Flimm Nov 20 '13 at 9:08
I have edited it in the answer, next time feel free to do the edit yourself. –  don.joey Nov 20 '13 at 10:06

First run the command shopt -s globstar. You can run that on the command line, and it'll have effect only in that shell window. You can put it in your .bashrc, and then all newly started shells will pick it up. The effect of that command is to make **/ match files in the current directory and its subdirectories recursively (by default, **/ means the same thing as */: only in the immediate subdirectories). Then:

rm **/*.bak

(or gvfs-trash **/*.bak or what have you).

share|improve this answer
find . -name "*.bak" -type f -print0 | xargs -0 /bin/rm -f
share|improve this answer
Welcome to askubuntu! While this is a perfectly valid answer I don't think there's any advantage in using this instead of the -delete flag of find. More information can be found in the GNU manuals entry for deleting files with find. –  Glutanimate Apr 4 '14 at 20:11
you are probably right, it's just an alternative solution, perhaps more raw ;) –  lokers Apr 4 '14 at 22:13
This is not just an alternative but an example how other commands can be combined together with the pipe '|'. +1 –  Boris Pavlović Jun 5 '14 at 7:18
This alternative solutions work on other environments that lack -delete (like cygwin) –  ciriarte Aug 15 '14 at 4:07

Simply do

rm *.bak

If you want to know what files you'll be deleting before delteting them, just run a ls *.bak. This will list all the files rm *.bak will delete.

This will only delete the files ending with .bak. Look up bash wildcards if you wanna do these kinds of things. Cheers!

share|improve this answer
This won't delete recursively, though (files in subfolders will remain untouched). –  Glutanimate Nov 20 '13 at 17:19

I think you can use:

rm -rf *.bak
share|improve this answer
This won't delete recursively, though (files in subfolders will remain untouched). –  Glutanimate Nov 20 '13 at 17:20
@Glutanimate it has -r, so surely that would delete recursively, right? (N.B. I'm still a newb to Unix/Linux CLI) –  Kieran Nov 20 '13 at 21:43
@crazyskeggy No, in case of rm recursive only means that it will delete both files and folders of that name, even if the folders have other content in them. But all of that still only in the working directory. –  Glutanimate Nov 21 '13 at 7:13

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.