find exec rm: Delete Files with find Command
The find command with -exec rm lets you delete files that match a search pattern, such as by name, age, or size. You enter the find search criteria and the rm command is run on each matching file. The result is a targeted deletion of only the files you specify, avoiding the risk of a broad rm -rf.
How the find -exec rm Command Works
The basic syntax is:
find [path] [conditions] -exec rm {} \;
Here, [path] is the directory to search (often . for the current directory), [conditions] filter which files are matched, and -exec rm {} runs rm on each result. The {} is a placeholder for the current file path, and \; terminates the command. A common alternative is -delete, which is simpler but less flexible—it cannot run other commands or use some predicates.
Why Use find -exec rm Instead of rm -rf
rm -rf deletes everything in a directory recursively without asking, which is dangerous if you mistype a path. find -exec rm lets you be selective: you can delete only files older than 30 days, only .tmp files, or only files larger than 100 MB. This reduces the chance of accidentally removing important data. It also works well with -name, -type, -size, and -mtime to target exactly what you want.
How to Choose the Right find Options
Selecting the right options depends on what you want to delete:
- By name:
-name "*.log"matches files ending in.log(case-sensitive). Use-inamefor case-insensitive. - By type:
-type ffor regular files,-type dfor directories. - By age:
-mtime +30finds files modified more than 30 days ago. Use-mminfor minutes. - By size:
-size +100Mfinds files larger than 100 megabytes. - Combine conditions: Use
-and(or just put them together) and-orfor more complex logic.
For example, to delete all .bak files in the current directory that are older than 7 days:
find . -name "*.bak" -type f -mtime +7 -exec rm {} \;
Common Mistakes and How to Avoid Them
One frequent error is forgetting the semicolon or the backslash before it. The command will fail with a syntax error. Another mistake is using -exec rm without testing first. Always run find without -exec first to see what will be matched. For example:
find . -name "*.tmp"
This lists the files. If the list looks correct, then add -exec rm {} \;. Also, be careful with spaces in filenames—the {} placeholder handles them correctly because find passes each path as a separate argument. However, if you use -exec sh -c 'rm "$1"' _ {}, you must handle quoting yourself.
Safety Tips: Dry Run and Confirmation
Before deleting anything, do a dry run by replacing -exec rm with -print to list the files. You can also add -ok instead of -exec to prompt for confirmation before each deletion:
find . -name "*.log" -ok rm {} \;
This asks < rm ... ./file.log > ? for each file. Answer y or n. This is safer for important operations, but it can be tedious for many files. Another safety measure is to use -type f to avoid accidentally deleting directories, unless you intend to remove them.
Alternatives to find -exec rm
Depending on your needs, you might use:
find -delete: A built-in action that deletes files directly. It is faster but has limitations: it cannot run other commands, and it fails on non-empty directories unless you use-depth.rmwith a list: If you have a list of files, you can usefind ... -print0 | xargs -0 rm. This is more efficient for many files but requires careful quoting.- Interactive selection: Use
find ... -exec rm -i {}to prompt for each file (same as-ok). - GUI file managers: For visual users, a file manager can search and delete, but it is not scriptable.
Performance and Efficiency Considerations
For a small number of files, -exec rm is fine. For thousands of files, spawning a new rm process for each file is slow. Use -exec rm {} + to batch multiple files into a single rm command. The plus sign replaces the semicolon and passes as many files as possible at once. Example:
find . -name "*.tmp" -exec rm {} +
This is much faster. However, be aware that -exec ... + may not work with all predicates, but it does with rm. Also, if you have a very large number of files, consider find ... -delete which is implemented in C and is the fastest.
Frequently Asked Questions
What does the semicolon do in find -exec rm?
The semicolon (;) tells find where the -exec command ends. It must be escaped with a backslash (\;) or quoted (';') to prevent the shell from interpreting it as a command separator. Without it, find will not know when to stop reading arguments.
Can I delete directories with find -exec rm?
Yes, but rm without -r cannot remove directories. To delete directories, use -exec rm -r {} \; or -exec rmdir {} for empty directories. Be careful: -r deletes recursively, so only use it when you intend to remove the directory and its contents.
How do I delete files older than a certain number of days?
Use the -mtime option. For example, find . -type f -mtime +30 -exec rm {} \; deletes files modified more than 30 days ago. Note that -mtime +30 means more than 30*24 hours old, not exactly 30 days. Use -mtime 30 to match files modified exactly 30 days ago (which is rare).
Is it safe to use find -exec rm without testing?
No. Always test with find ... -print first to see the list of files. Even then, double-check the path and conditions. A typo in the path or a missing -type f could delete unintended files. Use -ok for confirmation if you are unsure.
What is the difference between -exec and -delete?
-delete is a built-in action that deletes files directly, while -exec runs an external command. -delete is faster and simpler but cannot run other commands or use some predicates like -exec can. Also, -delete implies -depth, which affects directory traversal order.
Can I use find -exec rm with filenames that have spaces?
Yes. find passes each path as a single argument to rm, so spaces are handled correctly. The {} placeholder is replaced by the full path, and rm receives it as one argument. No special quoting is needed for the {} itself.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.