Linux Commands

Remove All Files with Extension Linux Command Line

Every file consists of a specific extension. If we want to delete files of the same or different extensions from our Linux system, we must follow many different types of commands. In this article, we will see how to remove all files with the extension Linux command line.

Remove All Files with Extension Linux Command Line

This section will explain different ways and methods to remove all files with extensions using the command line in Linux.

Using rm Command

The ‘rm’ command is a basic command-line utility in Linux to remove sockets, pipes, device nodes, symbolic links, directories, system files, etc. To remove a file with a particular extension, use the command ‘rm’. This command is very easy to use, and its syntax is something like this.

$ rm <filename1> <filename2> ... <filenameN>

In the appropriate command, ‘filename1’, ‘filename2’, etc., refer to the names, plus their full paths. When the files are in the same directory, we do not need to write the full path, but we must mention the full path if this is not the case.

We can use wildcard expressions to specify incremental, same-name files or files with specific file extensions. So, let’s take an example in which we will remove DATA.txt, DATA1.txt, and DATA2.txt files. These files are available in the Documents directory, so first, we will open them in the terminal using the following command:

After that, execute the below command

Now let’s verify that system has successfully deleted the files, so execute the following command:

Using Substring Remove Files

With the help of the following command, we can remove those files containing the substring ‘test’.

rm *test*

Here ‘*’ denotes any string. That’s why here ‘*test*“ considers all files named which have substring ‘test’.

We can easily remove files of particular extensions from any folder. We will delete files with the gif extension in this example. We can delete all GIF files from the folder using the following command.

rm *.gif

The above syntax only works for files. Along with files we can also delete folders using argument ‘-r’:

$ rm -r <file/folder1> <file/folder2> ... <file/folderN>

The main thing to note is that it deletes the folder as a whole recursively, i.e., all the files, subfolders, etc., of that folder in the entire folder structure. This concludes that there is no way to recursively delete files with specific extensions or files with filename patterns.

Find Command

Find is the most effective and popular command to search files. The find command is used to remove file extensions in Linux. The find command searches the files recursively based on size, extension, name, and file parameters. Using the find command, we can pipe its output to ‘rm’.

Backup and Verify Files

Different commands are also used to verify which files we are deleting, and their location is correct. However, this step is not required once it is right to be sure.

Backup files

For backup, we use the tar command. Unless we are 100% sure that we have the right files to delete or not, we should make a backup.

tar -cf backup.tar .

We can add the -v option to see the list of files that the tar command backs up.

tar -cvf backup.tar .

Remove files with find – delete

If we are hesitant while using ‘rm’, we can use ‘find’ apart from that. It has to be used with caution. It is something like this.

find . -name "*.bak" -type f -delete

First of all, we make sure which file we have to remove. For this, we use the following command.

find . -name "*.bak" -type f

We have to take special care that -delete is the last argument in our command. If by mistake we put it before the -name *.bak argument, it removes everything.

Remove files with find and xargs

It does not support the ‘-delete’ option. We can pipe it into ‘rm’ in the following way with the output that comes before us on searching.

find . -name "*.bak" | xargs rm

Here we pass an argument to ‘rm’ using the ‘xargs’ command. We can recursively remove the entire folder structure by using this method.

Removes files with find-exec

We use the rm with different commands (such as trash) or additional options to remove files.

find . -type f -name '*.txt~' -exec rm -f {} \;


find . -type f -name '*.txt~' -exec trash {} \;

Using find with -exec gives us the advantage of using any option and command to delete files. At the same time, it also allows us to perform other bulk operations on a set of files.


This article taught us how to delete any file from folders or folders with any specific extension by using various commands. We hope that from this article explained by us, you will get complete information in one place and you must have understood it very well.

About the author

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.