rm
Remove files and directories
By CMD Script Team · 4 min read · Last updated
rm [OPTIONS] FILE...Options
| Flag | Description |
|---|---|
-r | Recursively remove directories and their contents |
-f | Force removal without prompting, and ignore nonexistent files |
-i | Prompt for confirmation before every removal |
-v | Print a message for each file removed |
-d | Remove empty directories (without needing -r) |
--preserve-root | Refuse to operate recursively on / (the default in modern GNU coreutils) |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS
What it does
rm ("remove") deletes files, and with -r, entire directory trees. There is no
built-in trash or undo — once a file is removed, it's gone unless recovered through
filesystem-level tools or a backup. This makes rm, especially combined with -r and
-f, one of the most destructive commands in everyday use.
Beginner examples
rm file.txt— remove a single filerm file1.txt file2.txt— remove multiple files in one commandrm -i file.txt— prompt for confirmation before removingrm -r old_folder/— recursively remove a directory and everything inside itrm -v *.tmp— remove matching files while printing what was deleted
rm -i important_file.txt
Advanced examples
- Combine
-rand-fto force-remove a directory tree without any prompts:rm -rf node_modules/(extremely common but should always be double-checked before running). - Remove files matching a pattern while excluding others using
findfor more precise targeting than a shell glob allows:find . -name "*.bak" -type f -delete(safer thanrmfor complex matching, sincefind -deletedoesn't forkrmper file). - Remove only empty directories without needing full recursion:
rm -d empty_dir(equivalent in effect tormdirfor that case). - Use
-iselectively as a safety net for a wide glob you're not fully sure about:rm -i *.logprompts once per matched file rather than deleting them all silently.
find . -name "*.bak" -type f -delete
Common mistakes
- Running
rm -rfwith a mistyped or unintended path — especially with variable expansion in scripts, e.g.rm -rf "$DIR/"*where$DIRis accidentally empty, expanding torm -rf /*. - Assuming
rmmoves files to a trash/recycle bin — on Linux/macOS command-linerm, there is no trash; deletion is immediate and typically unrecoverable. - Forgetting
-rwhen trying to remove a directory and getting confused by the "is a directory" error, then reflexively adding-rfinstead of first confirming the directory's contents are actually meant to be deleted. - Using unquoted glob patterns or variables in
rmcommands, which can expand unexpectedly (including expanding to nothing, or to unintended files) and delete more or less than intended.
Tips
- Always double-check a
rm -rfcommand — especially one built from a variable path — by first runninglson the same path to confirm what would be affected. - Use
-ifor anyrmcommand involving a wildcard you're not 100% sure about; it adds a per-file confirmation prompt as a safety net. - Prefer
find ... -deleteover a glob-basedrmwhen the matching logic is complex (by name pattern, age, size), since it's more precise and avoids shell globbing pitfalls.
Best practices
- Never hardcode
rm -rfagainst a variable-built path in scripts without first validating the variable isn't empty or unexpected (set -uin bash helps catch unset variables early). - Keep regular backups or use a filesystem with snapshot support (e.g. Btrfs, ZFS, Time
Machine) for anything where accidental
rmwould be costly — there is no undo at the command level. - In production and CI environments, avoid running
rm -rfas root against broadly-scoped or dynamically-constructed paths; scope deletions as narrowly as possible and log what was deleted.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Cleaning up build artifacts and dependency directories like
node_modules/ordist/between builds:rm -rf node_modules dist. - Removing temporary or log files that have accumulated in a working directory:
rm -v *.tmp *.log. - Deleting an old, no-longer-needed project directory after confirming its contents are safe to discard.
Common interview questions
- Why is
rm -rf /so dangerous, and how do modern systems guard against it? It recursively force-deletes the entire filesystem with no prompts; modern GNU coreutils requires an additional--no-preserve-rootflag to actually target/directly, as a safety net (thoughrm -rfagainst any other important path is equally destructive and has no such protection). - Does
rmmove files to a trash bin? No — command-linermdeletes immediately and permanently; there's no built-in undo, unlike a GUI file manager's trash. - What's the difference between
rm -fand plainrm?-fsuppresses confirmation prompts and errors on nonexistent or write-protected files, making removal silent and non-interactive; plainrmmay prompt or fail in those cases.
Frequently Asked Questions
Why is rm -rf / considered so dangerous?
It recursively force-deletes the entire filesystem starting at root with no prompts, since -f suppresses confirmation and errors on missing files, and -r recurses into every directory. Modern GNU coreutils requires an extra --no-preserve-root flag to actually run this against / as a safety net, but rm -rf /some/important/path is just as destructive and has no such protection.
Is there an undo for rm?
Not built into rm itself. Once a file is unlinked, recovering it typically requires filesystem-level undelete tools (with no guarantee of success) or restoring from a backup/snapshot. This is why -i and careful use of -f matter.
What's the difference between rm -f and just rm?
Plain rm will prompt or error on write-protected files and will error if a file doesn't exist. rm -f suppresses those prompts and errors, silently succeeding (from the caller's perspective) even if the file was already gone.
Why does rm sometimes refuse to remove a directory?
Because rm requires -r (or -d for empty ones) to remove directories at all — a bare rm targeting a directory without -r fails with 'is a directory'.
Cheat sheet
Download a quick-reference cheat sheet for rm.