Introduction
On Linux, there are several ways to recursively remove files with a specific extension (.xyz) in a directory and its subdirectories. Deleting files recursively means deleting all files with the specified extension from the directory and all of its subdirectories. This operation can take a long time if done manually, especially for a large number of files. In this article, we will look at three methods to recursively delete files with a specific extension on Linux.
Using the rm command
The rm command is a basic file manipulation tool in Linux used to remove files and directories. It can be used in conjunction with other commands to get the desired result. To remove files with a specific extension recursively in a directory and its subdirectories, we can use the following command −
$ find /path/to/dir -name "*.extension" -type f -delete
Here, /path/to/dir is the path to the directory where you want to delete files, *.extension is the pattern for deleting files, -type f specifies that only files are to be deleted, and -delete is the action for delete files files.
For example, to remove all “.txt” files in the “/home/user/documents” directory and its subdirectories, the command would be −
$ find /home/user/documents -name "*.txt" -type f -delete
Using the find command with the rm command
The find command is a more advanced file manipulation tool in Linux which is used to find files and directories in a specific location. It can be used in conjunction with other commands to get the desired result.
To recursively delete files with a specific extension in a directory and its subdirectories, we can use the following command −
$ find /path/to/dir -name "*.extension" -type f -exec rm -f {} \;
Here, “/path/to/dir” is the path to the directory where you want to delete files, *.extension is the pattern to delete files, -type f specifies that only files should be deleted, -exec is used to execute a command and rm -f is the command to delete files with the -f option to force delete without warning. {} is a placeholder for the name of the file being processed and \; at the end of the command is used to terminate the command execution.
For example, to remove all ‘.txt’ files from the “/home/user/documents” directory and its subdirectories, the command would be −
$ find /home/user/documents -name "*.txt" -type f -exec rm -f {} \;
Using the for loop
Another way to recursively remove files with a specific extension in a directory and its subdirectories is to use a for loop in combination with the find command.
$ for file in $(find /path/to/dir -name "*.extension"); do rm -f "$file"; done
Here, /path/to/dir is the path to the directory where you want to delete files, *.extension is the pattern to delete files and “rm -f” is the command to delete files with the ‘-f’ option to force cancellation without notice. The “$(find…)” command is used to find files and the for loop is used to iterate through each file and delete it. For example, to remove all ‘.txt’ files in the “/home/user/documents” directory and its subdirectories, the command would be −
$ for file in $(find /home/user/documents -name "*.txt"); do rm -f "$file"; done
Note that this method is less efficient than the previous two methods, as it requires additional processing to build the file list and run the rm command for each file. However, it can be useful in certain situations where other methods may not work.
Using the find command directly
Another way to recursively delete files with a specific extension in a directory and its subdirectories is to use the find command directly with the “-delete” option.
$ find /path/to/dir -name "*.extension" -type f -delete
Here, /path/to/dir is the path to the directory where you want to delete files, *.extension is the pattern to delete files, -type f specifies to search only files (not directories), and -delete is the option to delete files. For example, to remove all .bak files from the current directory and its subdirectories, the command would be −
$ find . -name "*.bak" -type f -delete
Be aware that this method can be dangerous if not used carefully, as it will delete files immediately without warning. Before using this command, it is recommended that you run the following command to see a list of files that would be removed −
$ find . -name "*.bak" -type f
It is important to note that the ‘-delete’ option must be the last argument to the command. If it is placed before the “-name *.bak” argument, it will remove everything. Therefore, use this method with caution and make sure that you have a backup of all your important data before using it.
Conclusion
In this article, we discuss three methods to recursively delete files with a specific extension on Linux: using the rm command, using the find command, and using a for loop in combination with the rm command. Each method has its own syntax and can be used to achieve the desired result. Choose the method that best suits your needs and use it to save time and effort deleting files on Linux. Always remember to back up your data before attempting to delete files, especially when using a wildcard to match multiple files.
source : https://www.tutorialspoint.com/recursively-deleting-files-with-a-specific-extension-on-linux