Find Exec RM: What It Is & How to Use It
The calculator for this page is switched off while it is checked. The formula is below, and the worked example in the article shows how to use it by hand.
This article explains what the find exec rm command does in Linux and Unix-like systems, how to construct it safely, and how to avoid common pitfalls. You will learn the exact syntax, see worked examples, and get answers to frequent questions about deleting files with find.
Formula
The find command searches a directory tree for files and directories that match given criteria. The -exec option runs a command on each match. The general form is:
find [path] [conditions] -exec [command] {} \;
- path – the starting directory (e.g.,
.for current directory,/tmpfor absolute path). - conditions – filters like
-type f(files only),-name "*.log",-mtime +7(modified more than 7 days ago). - -exec – starts the command to run.
- {} – placeholder for each found file path.
- \; – terminates the command (the backslash escapes the semicolon from the shell).
So find . -type f -name "*.tmp" -exec rm {} \; deletes every .tmp file in the current directory and its subdirectories.
Worked Example
Using the calculator’s defaults (directory ., max age 7 days), the command becomes:
find . -type f -mtime +7 -exec rm {} \;
Step by step:
find .– start in the current directory.-type f– consider only regular files (not directories, symlinks, etc.).-mtime +7– keep files whose modification time is more than 7 days old (strictly greater than 7*24 hours).-exec rm {} \;– for each such file, runrmwith the file path replacing{}.
If there are three files older than 7 days, the command effectively runs rm file1, rm file2, rm file3. The calculator outputs the full command string you can copy and run.
When to Use find -exec rm
This pattern is ideal for batch deletions that would be tedious to do manually. Common use cases:
- Cleaning up old log files (e.g.,
*.logolder than 30 days). - Removing temporary files (
*.tmp,*~) from a project. - Deleting core dump files or cache directories.
- Purging files in a specific directory without touching other locations.
It is especially powerful in cron jobs or maintenance scripts where you want automated cleanup.
How to Choose the Right Options
Selecting the correct flags prevents accidental data loss. Key decisions:
- Type filter – Always use
-type funless you intend to delete directories. Without it,rmwill fail on directories (unless you use-r, which is dangerous). - Name pattern – Use
-nameor-iname(case-insensitive) to restrict to specific filenames. Example:-name "*.bak". - Time filters –
-mtime(modification),-atime(access),-ctime(change). Use+Nfor older than N days,-Nfor newer than N days,Nfor exactly N days. - Depth –
-maxdepth 1limits to the current directory only; without it, find recurses into subdirectories. - Dry run – Replace
rmwithechofirst to see what would be deleted:find . -type f -name "*.tmp" -exec echo {} \;.
Common Mistakes and How to Avoid Them
Several errors trip up beginners and experts alike:
- Missing backslash before semicolon –
find . -exec rm {} ;fails because the shell interprets;as a command separator. Always write\;or use+instead. - Using
-exec rm -rfwithout filters – This can wipe your entire directory tree. Always include-type fand a name pattern. - Forgetting to quote patterns – If your pattern contains wildcards, quote it (e.g.,
-name "*.log") so find expands it, not the shell. - Not testing first – Run with
echoor-lsto verify matches before deleting. - Using
-exec ... \;vs-exec ... +– The+variant batches multiple files into one command, which is faster and avoids command-line length limits. Example:find . -type f -name "*.tmp" -exec rm {} +.
Safety and Alternatives
Deleting files is irreversible in most systems (unless you use a trash system). Before running any find -exec rm command:
- Back up important data or move files to a temporary directory instead of deleting.
- Use
-deleteoption – Some find versions support-delete, which is safer because it refuses to delete non-empty directories. Example:find . -type f -name "*.tmp" -delete. - Use
-okinstead of-exec– Prompts for confirmation before each deletion:find . -type f -name "*.tmp" -ok rm {} \;. - Consider trash-cli – Tools like
trashmove files to a recycle bin instead of permanent deletion.
Also note that find follows symlinks only if you use -L; by default it does not, so symlinks to directories are not traversed.
Frequently Asked Questions
What does {} \; mean in find?
{} is replaced by each path that find outputs. The \; ends the command, with the backslash preventing the shell from treating ; as a command separator. Without it, find would not know where the -exec command ends.
How do I delete files older than X days?
Use -mtime +X for files modified more than X days ago. For example, to delete files older than 30 days: find /path -type f -mtime +30 -exec rm {} \;. Note that +30 means strictly greater than 30*24 hours, so a file modified exactly 30 days ago is not deleted.
Is find -exec rm the same as find -delete?
Not exactly. -delete is a built-in action that deletes files and empty directories, and it is generally safer because it refuses to delete non-empty directories. -exec rm runs the external rm command, which gives you more flexibility (e.g., you can use rm -i for interactive prompts). Performance-wise, -delete is often faster.
Why does my command say “find: missing argument to -exec�?
This usually happens when you forget the \; or + at the end of the -exec clause, or when you put a space between {} and \; incorrectly. The correct syntax is -exec rm {} \; (no space between {} and \; is required, but a space is fine as long as the semicolon is escaped).
Can I use find -exec rm on Windows?
Windows does not have a native find command with -exec. However, if you use Windows Subsystem for Linux (WSL), Git Bash, or Cygwin, you can run the same commands. Alternatively, PowerShell has Get-ChildItem | Remove-Item for similar tasks.
How do I preview what will be deleted?
Replace rm with echo to print the file paths instead of deleting: find . -type f -name "*.tmp" -exec echo {} \;. Or use find . -type f -name "*.tmp" -ls to list them with details.
Related
Formula used
in d | Directory path | - | . | 0
in a | Max age (days) | days | 7 | 0
out cmd | Command to run | - | "find " + d + " -type f -mtime +" + a + " -exec rm {} ;" | 0Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.