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
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.
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:
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:
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
- * Match one or number of more occurence
- ? Match a single occurrence
-  Specify a range for matching
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.
And I’ll show you how to find the correct file, and once I’m sure of the result, I’ll append
xargs to delete the result, which is the safest way to delete files using the
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:
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?
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.
A man with a tech effusive who has explored some of the amazing technology stuff and is exploring more. While moving towards, I had a chance to work on Android development, Linux, AWS, and DevOps with several open-source tools.