How To Find And Delete Files That Contains A Specific Text In Their Names In Linux

Find And Delete Files That Contains A Specific Text In Their Names

I have a load of PDF files that have some important Linux tips and tricks. I gave those files an unique keyword as file name for easier identification. Say for example, I saved the files that contains LDAP server configuration details as ldapconfig.pdf, FTP configuration file as ftpconfig.pdf, DHCP server configuration details as dhcp_config.pdf, and so on. I converted most of them to articles and posted in our website, so that other users can also benefit from them. The problem is those files are consuming lot of hard disk space. Since, I have hundred of files, I don’t want to manually find and delete them one by one. It might take some hours to find and delete all files.

While looking for an easy solution, I came to know that “find” command can help me in this case. After going through find command’s man pages, I found a suitable solution. For those who are looking for an easy way to find and delete files that contains a specific text or string in their names, read on. It is not that difficult.

Find And Delete Files That Contains A Specific Text In Their Names In Linux

Have a look at the following example. I have the following files in a directory called “Linux_guides”.

$ cd Linux_guides/
$ ls
apache_config.pdf dhcp_config.pdf ldap_config.pdf php_install.pdf
apache_install.pdf ftp_config.pdf mysql_install.pdf

I completed the installation part, so I don’t need the files that contains words “install” in their filenames.

Instead of finding and deleting the files that contains the phrase “install” one by one, we can delete all of them at once using the following command:

find -type f -name '*install*' -delete

The above command will delete all files that have the phrase “install” in their name and keep everything else.

Find And Delete Files That Contains A Specific Text In Their Names

If delete option is not available, you can use any one of the following command:

find -type f -name '*install*' -exec rm {} \;

Or,

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

You might ask we can do the same by using “rm” command like below:

rm *install*

Yes, it will do. However, rm *install* command will remove all files containing “install” in filename from current directory and will complain if any directory name contains “install”. But, “find -type f” command will recursively search for only files in current directory and all sub-directories.

If you only want to find files (not delete), just remove the options -delete or -exec rm {} \; or -exec rm {} + in the above commands. For example, the following command will only find the files that contains the string “install” in their names:

$ find -type f -name '*install*'
./mysql_install.pdf
./apache_install.pdf
./php_install.pdf

Find And Delete Files That does not contains s specific text in their names in Linux

You know how to delete a file that contains a specific phrase in its name. Let us now see how to find and delete files that doesn’t have a specific phrase or string in their names.

To do so, just add ‘-not’ parameter in the find command as shown below.

find -type f -not -name '*install*' -delete

The above command will not delete the files that have the phrase “install” in their name and delete everything else.

If delete option is not available, you can use any one of the following commands:

find -type f -not -name '*install*' -exec rm {} \;

Or,

find -type f -not -name '*install*' -exec rm {} +

Find And Delete Files That Contains A Specific Text In Their Names

For more details about find command, refer man pages.

man find

And, that’s all for now. You now know to find a file that contains a specific name in its name and how to delete a file that has/hasn’t a specific word in its name. This will be helpful when you want to delete large number of files of same type in a directory. Hope this helps. More good stuffs to come. Stay tuned!

Cheers!

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You may also like...