The rm command in Linux
rm removes files and directories. Unlike deleting from a graphical file manager, rm does not move anything to a trash bin: once removed, a file is gone. That makes it worth slowing down for a second before pressing Enter, especially when the command contains -r, -f or a wildcard.
How rm works
rm does not delete data. It calls unlink(), which removes one name from a directory and decrements the file's link count. Only when the count reaches zero — no names left anywhere — does the filesystem mark the blocks free. "Remove" in Unix is precisely "unlink a name", which is why the permission that governs deletion is write permission on the directory, not on the file itself.
There is a second condition for space to be released: no process may still hold the file open. A deleted-but-open file keeps occupying disk until the last file descriptor closes — the classic explanation for "I deleted the huge log but df shows no free space". And recovery after rm is unreliable precisely because the blocks return to the free pool: the next write may claim them at any moment.
Syntax
rm [OPTIONS] FILE... Common options
| Option | What it does |
|---|---|
-r | Recursive: remove directories and everything inside them. |
-f | Force: never prompt, and ignore files that do not exist. |
-i | Interactive: ask for confirmation before each removal. |
-v | Verbose: print each file as it is removed. |
-d | Remove empty directories (equivalent to rmdir). |
How to use rm: examples
$ rm draft.txt Remove a single file.
$ rm *.log Remove every file ending in .log in the current directory.
$ rm -r old-project Remove a directory and all of its contents, recursively.
$ rm -rf build/ Remove a directory tree without any prompts. Powerful and dangerous — double-check the path before running it.
$ rm -i important-* Ask for confirmation file by file — a good habit when using wildcards.
Real-world use cases for rm
Cleaning build artifacts
Before a fresh build: rm -rf dist/ node_modules/.cache removes generated directories entirely. The -f matters in CI: it stays silent when the directories don't exist yet, so the pipeline doesn't fail on the first run.
Pruning by pattern — carefully
Freeing disk space from old logs: first preview with ls *.log.2025-*, and only when the listing shows exactly what you expect, replace ls with rm. Previewing the glob with ls first is the habit that prevents the famous disasters.
Try rm yourself
This is a live sandbox with a small filesystem (documents/, notes.txt,
backup.sh…). Nothing you do here can break anything — experiment freely.
Try: rm -r downloads
Pro tips and common mistakes
- The single most important rm habit: pause and re-read any command containing -rf before pressing Enter. Especially check for stray spaces — rm -rf / tmp/x (with the space) is catastrophic.
- Prefer deleting via find for anything conditional: find . -name "*.tmp" -mtime +30 -delete removes only old temp files, and you can run it without -delete first as a preview.
- rm -- -weirdfile deletes a file whose name starts with a dash; the -- marks the end of options.
- On your own machine, consider trash-cli (trash command) for interactive use — it gives you an undo that rm will never have.
Frequently asked questions about rm
How do I delete a directory with rm?
Plain rm refuses directories. Use rm -r dirname to remove the directory and everything inside it. For an empty directory you can also use rmdir.
Can I recover a file deleted with rm?
In practice, no — there is no trash bin, and recovery tools are unreliable on modern filesystems. Treat rm as permanent and keep backups of anything important.
What does rm -rf actually mean?
-r removes directories recursively and -f suppresses all prompts and errors. Combined, they delete an entire tree instantly and silently — which is why rm -rf on the wrong path is the classic catastrophic mistake.
Related commands
Create one or many directories, including whole nested paths with -p.
cpCopy files and whole directory trees, preserving attributes when needed.
mvMove files between directories and rename them — the same operation in Linux.
findLocate files anywhere in a directory tree by name, type, size, date — and act on them.