>_cmd.script

rm

Remove files and directories

Files

By CMD Script Team · 4 min read · Last updated

SYNTAX
rm [OPTIONS] FILE...

Options

Command options and flags
FlagDescription
-rRecursively remove directories and their contents
-fForce removal without prompting, and ignore nonexistent files
-iPrompt for confirmation before every removal
-vPrint a message for each file removed
-dRemove empty directories (without needing -r)
--preserve-rootRefuse 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 file
  • rm file1.txt file2.txt — remove multiple files in one command
  • rm -i file.txt — prompt for confirmation before removing
  • rm -r old_folder/ — recursively remove a directory and everything inside it
  • rm -v *.tmp — remove matching files while printing what was deleted
rm -i important_file.txt

Advanced examples

  • Combine -r and -f to 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 find for more precise targeting than a shell glob allows: find . -name "*.bak" -type f -delete (safer than rm for complex matching, since find -delete doesn't fork rm per file).
  • Remove only empty directories without needing full recursion: rm -d empty_dir (equivalent in effect to rmdir for that case).
  • Use -i selectively as a safety net for a wide glob you're not fully sure about: rm -i *.log prompts once per matched file rather than deleting them all silently.
find . -name "*.bak" -type f -delete

Common mistakes

  • Running rm -rf with a mistyped or unintended path — especially with variable expansion in scripts, e.g. rm -rf "$DIR/"* where $DIR is accidentally empty, expanding to rm -rf /*.
  • Assuming rm moves files to a trash/recycle bin — on Linux/macOS command-line rm, there is no trash; deletion is immediate and typically unrecoverable.
  • Forgetting -r when trying to remove a directory and getting confused by the "is a directory" error, then reflexively adding -rf instead of first confirming the directory's contents are actually meant to be deleted.
  • Using unquoted glob patterns or variables in rm commands, 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 -rf command — especially one built from a variable path — by first running ls on the same path to confirm what would be affected.
  • Use -i for any rm command involving a wildcard you're not 100% sure about; it adds a per-file confirmation prompt as a safety net.
  • Prefer find ... -delete over a glob-based rm when 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 -rf against a variable-built path in scripts without first validating the variable isn't empty or unexpected (set -u in 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 rm would be costly — there is no undo at the command level.
  • In production and CI environments, avoid running rm -rf as 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/ or dist/ 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-root flag to actually target / directly, as a safety net (though rm -rf against any other important path is equally destructive and has no such protection).
  • Does rm move files to a trash bin? No — command-line rm deletes immediately and permanently; there's no built-in undo, unlike a GUI file manager's trash.
  • What's the difference between rm -f and plain rm? -f suppresses confirmation prompts and errors on nonexistent or write-protected files, making removal silent and non-interactive; plain rm may 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.