Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Files and Directories

Linux rm command

Reference for the Linux rm command: delete files and directories, rm -r and -f, why there is no undo, protecting against mistakes and safer alternatives.

rm removes names from the filesystem. If a file has no other names (hard links) and no process has it open, its data is freed. There is no recycle bin and no undo: once rm has run, the file is gone for practical purposes.

By default rm deletes only files. Directories need -r (recursive), and -f suppresses errors and prompts. The combination rm -rf is powerful and dangerous, so quote carefully, look at what a wildcard expands to, and never run it with a variable that might be empty.

Terminal
rm [OPTION]... FILE...

Try it

Bash Files, directories and options

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Safety: previewing, dashes and empty variables

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
rm [-dfirvI] [--one-file-system] [--preserve-root] FILE...
Option Description
-r, -R Remove directories and their contents recursively.
-f Ignore nonexistent files and never prompt.
-i Prompt before every removal.
-I Prompt once before removing more than three files or when recursing.
-d Remove empty directories (like rmdir).
-v Print each removal.
--one-file-system In recursive mode, skip directories on other filesystems.
--preserve-root Refuse to operate recursively on / (default in GNU rm).

Returns: 0 if all files were removed (or, with -f, did not exist); non-zero if any removal failed.

Usage notes

  • Deleting needs permission on the directory, not on the file: write and execute on the parent directory. A read-only file in a writable directory is removed (rm asks first on a terminal, unless -f).
  • No undo. Deleted data may be recoverable with forensic tools if nothing overwrites it, but do not count on that. Keep backups; consider a trash tool such as trash-put for interactive use.
  • Hard links: rm removes one name. The data stays while another name or an open file descriptor exists. A deleted log file that a daemon still writes keeps using disk space until the daemon closes it.
  • Always expand first. Run ls or echo with the same pattern before rm. A stray space in rm -rf /tmp/ foo deletes /tmp/ and foo.
  • Empty variables are dangerous. rm -rf "$DIR"/* becomes rm -rf /* if DIR is empty. Use ${DIR:?} to make the shell stop, or set -u.
  • Names starting with a dash need -- or a path prefix (./-file). Names with spaces need quotes.
  • Symlinks: rm link removes the link, never the target. rm link/ (trailing slash) is an error or acts on the target directory contents; do not use it.
  • Many files: a glob can exceed the argument limit ("Argument list too long"). Use find dir -type f -delete or find ... -print0 | xargs -0 rm.
  • Removing a directory tree is faster with rm -rf than with find -exec rm, and rm -rf dir also removes dir itself, unlike rm -rf dir/*.
  • Secure deletion is not what rm does; shred overwrites a file first, with limited value on journaled, SSD or copy-on-write filesystems.