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.
rm [OPTION]... FILE...
Try it
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.
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
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 (
rmasks 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-putfor interactive use. - Hard links:
rmremoves 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
lsorechowith the same pattern beforerm. A stray space inrm -rf /tmp/ foodeletes/tmp/andfoo. - Empty variables are dangerous.
rm -rf "$DIR"/*becomesrm -rf /*ifDIRis empty. Use${DIR:?}to make the shell stop, orset -u. - Names starting with a dash need
--or a path prefix (./-file). Names with spaces need quotes. - Symlinks:
rm linkremoves 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 -deleteorfind ... -print0 | xargs -0 rm. - Removing a directory tree is faster with
rm -rfthan withfind -exec rm, andrm -rf diralso removesdiritself, unlikerm -rf dir/*. - Secure deletion is not what
rmdoes;shredoverwrites a file first, with limited value on journaled, SSD or copy-on-write filesystems.
Related
rmdir,mv,find,ln- Lessons: Copying, moving and deleting, Wildcards and globbing