Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I need to find all files starting with the name NAME in a directory tree and remove all these files using one shell command.

share|improve this question

migrated from stackoverflow.com Oct 2 '12 at 13:40

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

3 Answers 3

up vote -2 down vote accepted

to delete all files which name has name--- you can use

find -name . 'name*' -exec rm {} \;
share|improve this answer
    
You can also add -f as an 'rm' argument so you don't get prompted for "Are you sure you want to remove X file?" –  UtahJarhead Oct 2 '12 at 14:01
    
Which version of grep has an -exec switch? –  Ben Graham Oct 3 '12 at 2:55
2  
How did this get to be the accepted answer ? The syntax for find is completely wrong. –  Paul R Oct 24 '12 at 13:18

You can use find:

find . -name "name*" -exec rm {} \;
share|improve this answer
    
Also instead of specifying '.' you can specify an absolute path. –  UtahJarhead Oct 2 '12 at 14:02

Delete all files in current directory and its sub-directories where the file name starts with "foo":

$ find . -type f -name foo\* -exec rm {} \;

NB: use with caution - back up first - also do a dry run first, e.g.

$ find . -type f -name foo\*

will just tell you the names of the files that would be deleted.

share|improve this answer
2  
+1 for also providing the "safe" version. –  crmpicco Aug 16 '13 at 10:54
1  
I had to delete over 2Million files and run in to trouble, find . -type f -name foo\* -delete did the trick –  Linas Jan 25 at 13:30

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.