Tell me more ×
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

3 Answers

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
How did this get to be the accepted answer ? The syntax for find is completely wrong. – Paul R Oct 24 '12 at 13:18

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

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

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.