How to Delete Files with Specific Extensions from the Linux Command Line

Deleting files one by one is not worth the effort if you have a large number of files with the same extension or a similar pattern of files that you need to remove from your system.

You can save yourself a lot of time by using commands that allow you to delete all files sharing the same extension or pattern at once. For example, you could use a wildcard character like "^","*","?","[]" in conjunction with the find and rm commands to delete the files that match the given criteria.

So let’s start this guide by removing some of the files using a wildcard character.

Remove files using a wildcard character in Linux

As you may be aware, the rm command accepts one or more arguments to remove files from your system; however, instead of specifying multiple filenames, you can use wildcard characters such as “^, *,?, []” to remove multiple files without specifying multiple filenames.

Let me demonstrate how to delete multiple files at once using a wildcard character. If you look closely at the image below, you may be able to deduce that I have created multiple files with a certain pattern.

Remove file using wild card pattern
Using a wildcard pattern, remove the file

Invoking the following command will allow me to only delete the file that contains an extra character after “foo_”, while leaving the rest of the file intact:

$ rm -v foo_*

The above command produces the following result:

Removed multiple files using wild card
Removed multiple files using wild card

If you want me to remove the “.txt” file that contains “foo” as a substring, no matter where “foo” is presented in the filename, then I need to append “*” in between “foo” and close with “*”, which deletes all the files that have “foo” in their filename, and to do this, I’ll run the below command:

$ rm -v *foo*.txt

And if you want me to give you more examples, then so be it. I’m more than happy to provide additional examples if you think it would be helpful.

Let’s look at another example where I’ll use the following command to delete any file that does not begin with “foo” by specifying the range with square brackets:

$ rm -v _*[a-z].txt

Can you guess why I have used the asterisk (*) character in this pattern? If you didn’t get it, then ask me in the comment section, and here is the output of the above command:

Remove file which is not starting with foo
Remove any files that do not begin with foo

I can play more with the wildcard, but I won’t because it’s your time to play with the wildcard to remove the file, and one thing I would recommend you do is practice in a new directory to prevent deleting unnecessary files.

I’m also including a small cheat sheet with an example for your convenience, so you can refer to it when you’re not sure how to use the wild card:

  • ^ Exclude from match
    • $ rm -v [^foo]*
  • * Match one or number of more occurence
    • $ rm -v _*
  • ? Match a single occurrence
    • $ rm -v foo_???.txt
  • [] Specify a range for matching
    • $ rm -v [^a-h]*.txt

Now let’s move on to the next section, where you will learn how to find and recursively delete all files of a specific extension from the current directory.

Remove all files with extension files in Linux

Above, you learned to remove files with a specific pattern, which deletes the file as per the given pattern, and now we will use the same wild cards to remove files recursively through the multiple directories in one go.

You will use the find command for this purpose, which is one of my favourites because it can be used to find almost anything using a wide variety of options. As a result, I wrote an article on 20+ find commands that you can use on a daily basis, so please check it out if you want to learn them.

For demonstration purposes, I have created the directory and subdirectory and have moved some of the PNG-based extension files to all the directories, as shown in the below image.

Show number of files in current directory using tree command
Show the number of files in the current directory using the tree command

And I’ll show you how to find the correct file, and once I’m sure of the result, I’ll append -delete or xargs to delete the result, which is the safest way to delete files using the find command.

On a terminal, run the following command to recursively find all the png files from the current directory, and once the find command locates the file, it will print the file path on a screen:

$ find . -name "*.png"

The following is the result of the above command:

find command result
find command result

Once you’re happy with the result, you can add the -delete option to get rid of the file. After running the command, the file will be gone from your system, so it’s best to be careful.

$ find . -name "*.png" -delete 

If the above command doesn’t work, then try this one:

$ find . -name "*.png" | xargs rm 

Incredibly easy, right?

Wrap up

And with that, I must adjourn. The article “Linux rm command with advanced syntax for Pro” discusses some of the other methods you can use to delete files from your computer.

Anyway, if there is something I need to consider, please use the space below to provide any additional information or feedback.

Till then, put your feet up and relax. As always, the next article will be up shortly.

Leave a Reply