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

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

By sk
Published: Last Updated on 23.4K views

This guide explains how to find and delete files that contains a specific text or string in their names using find command in Linux. In addition, we will also learn how to find and delete files that "does not" contain specific text in their names.

Introduction

I have many PDF files which have some important Linux tips and tricks. I gave those files an unique 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 here in our blog, so that other users can also benefit from them.

The problem is those files are consuming a lot of disk space. Since, I have hundreds 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. Read on.

1. 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 In Linux
Find And Delete Files That Contains A Specific Text In Their Names In Linux

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

$ 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, the "find -type f" command will recursively search for only files in current directory and all sub-directories.

1.1. Find Files That Contains A Specific Text In Their Names In Linux

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*'

Sample output:

./mysql_install.pdf
./apache_install.pdf
./php_install.pdf

2. Find And Delete Files That "does not" contain 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 Dost Not Contain A Specific Text In Their Names In Linux
Find And Delete Files That Dost Not Contain A Specific Text In Their Names In Linux

For more details about find command, refer man pages.

$ man find

Conclusion

You now know to find a file that contains a specific name in its name and how to delete a file that has and hasn't had 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.

You May Also Like

1 comment

Zahid Ullah March 6, 2020 - 10:50 pm

Hmmm nice but am looking for a way to delete a file if it contain specific string inside it , i wonder how can we do that

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More