58

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

21

To delete all files which name has name, you can use it:

find  . -name 'name*' -exec rm {} \;
  • 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
  • Why does this comment have downvotes? – Ultrasonic54321 Jan 30 '19 at 17:07
  • @Ultrasonic54321: probably because it's much the same as the earlier answers, and doesn't really add anything new. – Paul R Dec 19 '19 at 17:47
  • In order to work for me (perhaps it is a timely change in the version) I left the negative (-) out of the -name, such as just 'name', then it worked. Thank you for the solution! – Mugé May 4 '20 at 15:54
77

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.

  • 7
    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
45

I have tried this way it is working for me try below command.

rm -rf Example*

here "Example" is text which is common for all files.

  • 8
    Not sure why this isn't the highest-voted answer.... – DanTheMan May 22 '19 at 18:38
  • @DanTheMan It's because it's ~4 years newer than the other answers. This is definitely the most simple answer though. – joshuakcockrell Jul 17 '19 at 23:46
  • This is my go-to to removing files like logs that build up. So much simpler than the "find" method that has been voted. – Art Geigel Sep 4 '20 at 17:03
  • Plus, you can use tab to complete to make sure you spelled it right. Awesome. – Corey Levinson Sep 11 '20 at 12:00
11

You can use find:

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

With the globstar option (enable with shopt -s globstar):

rm -f **/NAME*

**/ expands to ./, */, */*/, */*/*/ etc. If you have a directory name starting with NAME, the command will complain that rm can't remove directories, but that's all.

Notice that this might run into command line length limitations if the glob matches many files.

Alternatively, with as few invocations of rm as possible, but not subject to any command line length limitations:

find . -type f -name 'NAME*' -exec rm -f {} +

(Notice the + instead of \; to close the -exec statement.)

2

find . -name 'foo'* -type f -delete seems like the simplest answer.

You can run this without the -delete flag before to see which files will be deleted.

-1

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 {} \;
  • This is a good answer — to a different question.  It’s not an answer to this question. – Scott Oct 26 '18 at 4:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.