Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

up vote -4 down vote accepted

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

find -name . 'name*' -exec rm {} \;
share|improve this answer
1  
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
4  
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
3  
+1 for also providing the "safe" version. – crmpicco Aug 16 '13 at 10:54
2  
I had to delete over 2Million files and run in to trouble, find . -type f -name foo\* -delete did the trick – Linas Jan 25 '14 at 13:30

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

Search for the "Inode" number of the file/folder and then delete using inode number. Below is an example:

ls -il
3407873 drwxr-xr-x. 2 root   root      4096 Mar 30 07:49 –p

find . -inum 3407873 -exec rm -rf {} \;
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .