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:

OptionEffect
-r, -R, --recursiveRemove directories and all of their contents, recursively.
-d, --dirRemove 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 #

OptionEffect
-f, --forceNever prompt. Also ignore files that do not exist instead of reporting them.
-iPrompt before every removal.
-IPrompt 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.

OptionEffect
--preserve-rootRefuse to recurse on /. This is the default; you do not need to ask for it.
--no-preserve-rootDisable the failsafe. Required, in full and unabbreviated, if you genuinely intend a recursive operation on /.

Other options #

OptionEffect
-v, --verbosePrint each file as it is removed.
--progressShow 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 #

CodeMeaning
0Every requested removal succeeded (or, with -f, the file did not exist).
1A file could not be removed.

Edit this page