How to remove all files starting with a certain string in Linux

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

0

7 Answers

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

find . -name 'name*' -exec rm {} \;
6

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.

1

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.

5

You can use find:

find . -name "name*" -exec rm {} \;
1

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.

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 {} \;
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like