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


7 Answers 7


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?" Oct 2, 2012 at 14:01
  • Which version of grep has an -exec switch?
    – Ben Graham
    Oct 3, 2012 at 2:55
  • Why does this comment have downvotes? Jan 30, 2019 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, 2019 at 17:47
  • 1
    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, 2020 at 15:54

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.

  • 10
    I had to delete over 2Million files and run in to trouble, find . -type f -name foo\* -delete did the trick
    – Linas
    Jan 25, 2014 at 13:30

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.

  • 15
    Not sure why this isn't the highest-voted answer....
    – DanTheMan
    May 22, 2019 at 18:38
  • @DanTheMan It's because it's ~4 years newer than the other answers. This is definitely the most simple answer though. Jul 17, 2019 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, 2020 at 17:03
  • Plus, you can use tab to complete to make sure you spelled it right. Awesome. Sep 11, 2020 at 12:00
  • 1
    This shouldn't be the highest-voted answer because it doesn't always work. Imagine if you have files in subfolders like A/Example1, B/Example2, C/Example3. You'd have to cd into each subfolder, or else it'll only search the root folder and not find anything. Mar 11, 2021 at 7:25

You can use find:

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

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.

  • 2
    As pointed out by @Linas find . -type f -name foo\* -delete works with files over 1M+, other solutions failed with ` Argument list too long`
    – Yatko
    Jul 31, 2022 at 22:45

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.)


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. Oct 26, 2018 at 4:33
  • 1
    But it answered the question I had when I searched and found this one. Are we so pedantic we can't have closely-related answers? Short on storage space? I hope the SO and related are useful repositories - not exemplary samples of rule-following.
    – Art Swri
    Feb 26, 2023 at 23:53

You must log in to answer this question.

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