Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Recursively Deleting Files With a Specific Extension on Linux
On Linux, there are several ways to recursively remove files with a specific extension from a directory and its subdirectories. Deleting files recursively means deleting all files with the specified extension from the current directory and all nested subdirectories. This operation can be time-consuming if done manually, especially for a large number of files. This article explores four effective methods to recursively delete files with a specific extension on Linux.
Using the find Command with -delete Option
The most efficient method uses the find command with the built-in -delete option. This approach is fast and doesn't require spawning additional processes.
find /path/to/dir -name "*.extension" -type f -delete
Here, /path/to/dir is the target directory path, *.extension is the file pattern to match, -type f ensures only files (not directories) are processed, and -delete removes the matched files.
Example
find /home/user/documents -name "*.txt" -type f -delete
Important: Always test your command first by omitting -delete to preview which files will be affected:
find /home/user/documents -name "*.txt" -type f
Using find with -exec and rm
The find command can execute rm for each matched file using the -exec option. This method provides more control and can handle complex scenarios.
find /path/to/dir -name "*.extension" -type f -exec rm -f {} \;
Here, -exec executes the following command, rm -f forces deletion without prompts, {} is a placeholder for each found file, and \; terminates the exec command.
Alternative with Plus Sign (More Efficient)
find /path/to/dir -name "*.extension" -type f -exec rm -f {} +
Using + instead of \; passes multiple files to a single rm command, making it more efficient for large file sets.
Using a for Loop with Command Substitution
A for loop can iterate through files found by the find command. However, this method is less efficient and should be avoided for directories with many files.
for file in $(find /path/to/dir -name "*.extension" -type f); do
rm -f "$file"
done
Note: This method has limitations with filenames containing spaces and is generally not recommended for production use.
Using xargs for Better Performance
The xargs command provides an efficient way to handle large numbers of files by batching them into manageable groups.
find /path/to/dir -name "*.extension" -type f -print0 | xargs -0 rm -f
The -print0 option separates filenames with null characters, and -0 tells xargs to expect null-separated input. This method handles filenames with spaces correctly.
Safety Considerations
| Method | Safety Level | Performance | Best Use Case |
|---|---|---|---|
| find -delete | High (with testing) | Excellent | Simple, fast deletions |
| find -exec rm | High | Good | Complex conditions |
| for loop | Medium | Poor | Small file sets only |
| find | xargs | High | Excellent | Large file sets |
Practical Examples
Remove all backup files from the current directory and subdirectories:
find . -name "*.bak" -type f -delete
Remove log files older than 7 days:
find /var/log -name "*.log" -type f -mtime +7 -delete
Remove temporary files with confirmation:
find /tmp -name "*.tmp" -type f -exec rm -i {} +
Conclusion
The find command with -delete option provides the most efficient method for recursively deleting files by extension. Always test commands without the deletion action first, maintain backups of important data, and choose the method that best fits your specific requirements and safety needs.
