On this page
rm
rm removes files. With -r, it removes directories and everything inside them.
rm [options] file...
$ rm draft.txt old-notes.txt
$ rm -r build/
Removal is not reversible — once rm removes a file, its directory entry is gone. rm has a few safety behaviours, below, but the basic rule stands: think before you confirm.
Removing directories
rm will not remove a directory unless you say so:
| Option | Effect |
|---|---|
-r, -R, --recursive |
Remove directories and all of their contents, recursively. |
-d, --dir |
Remove a directory, but only if it is already empty. |
The write-protected prompt
By default — when you have not passed -f and rm is running interactively — rm prompts before removing a file you cannot write to:
$ rm policy.db
rm: remove write-protected regular file 'policy.db'?
The point of that prompt is to catch mistakes: a file you cannot write is one you most likely did not mean to delete.
"Write-protected" here means a real access check. rm decides it by asking the kernel whether your token may write the file — a live check against the file's security descriptor. It is not read off any decorative metadata on the inode; it is the same authority question that any write to the file would face. So the prompt is honest: if rm says a file is write-protected, it is a file the access model would genuinely stop you writing.
(The check is about write access to the file's contents. Whether you may actually remove the file is a separate question, decided by its own access right — so you can be prompted about a write-protected file and still be allowed to delete it. The prompt is a courtesy check, not the authorisation.)
Controlling the prompts
| Option | Effect |
|---|---|
-f, --force |
Never prompt. Also ignore files that do not exist instead of reporting them. |
-i |
Prompt before every removal. |
-I |
Prompt just once before removing more than three files or before a recursive removal. Less intrusive than -i, but still a check against a slip. |
--interactive[=WHEN] |
Set the prompting explicitly: never, once, or always. |
The root failsafe
rm refuses to recursively remove / — the root of the file system — because doing so by accident would be catastrophic.
| Option | Effect |
|---|---|
--preserve-root |
Refuse to recurse on /. This is the default; you do not need to ask for it. |
--no-preserve-root |
Disable the failsafe. Required, in full and unabbreviated, if you genuinely intend a recursive operation on /. |
Other options
| Option | Effect |
|---|---|
-v, --verbose |
Print each file as it is removed. |
--progress |
Show a progress bar. |
rm and shred
rm unlinks a file — it removes the name, and the space becomes free. The file's data may still be physically present on the device until something else overwrites it. When you need the contents to be genuinely unrecoverable, use shred, which overwrites the data before removing the file.
Exit status
| Code | Meaning |
|---|---|
0 |
Every requested removal succeeded (or, with -f, the file did not exist). |
1 |
A file could not be removed. |