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
add comment

6 Answers 6

up vote 41 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
    
Oh yeah, I didn't realize that. –  Bolt64 Nov 15 '13 at 13:15
6  
@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
show 8 more comments

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
1  
"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
add comment

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
add comment
find . -name "*.bak" -type f | xargs /bin/rm -f
share|improve this answer
1  
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 at 20:11
    
you are probably right, it's just an alternative solution, perhaps more raw ;) –  lokers Apr 4 at 22:13
1  
This is not just an alternative but an example how other commands can be combined together with the pipe '|'. +1 –  Boris Pavlović Jun 5 at 7:18
add comment

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
1  
This won't delete recursively, though (files in subfolders will remain untouched). –  Glutanimate Nov 20 '13 at 17:19
add comment

I think you can use:

rm -rf *.bak
share|improve this answer
1  
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) –  crazyskeggy Nov 20 '13 at 21:43
1  
@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
add comment

Your Answer

 
discard

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.