rm
Overview
rm removes directory entries (unlinks files). By default it does not remove directories unless -r is given. There is no undelete in coreutils — recovery requires backups or filesystem tools.
Syntax
rm [options] file...Common Options
| Option | Description |
|---|---|
-r / -R |
Recursive (directories) |
-f |
Force; ignore missing, no prompts |
-i |
Prompt each |
-I |
Prompt once if many/recursive |
-v |
Verbose |
--one-file-system |
Do not recurse into other mounts |
--preserve-root |
Default: refuse / recursive remove |
Safety
Never experiment with rm -rf / or unquoted globs as root. Prefer: - trash-cli / file manager trash when available
- Move to a quarantine dir first
- find … -print dry-run before -delete
Refuse muscle-memory alias rm='rm -i' as your only protection — scripts bypass aliases.
Examples with Explanations
Files
rm file.txt
rm -v file1 file2Interactive
rm -i *.logDirectory tree
rm -r build/
rm -rf build/ # no prompt; double-check pathRefuse accidental root
rm -rf / --preserve-root # still refused; do not try variantsSafer recursive pattern
# dry run
find ./tmpdir -type f -name '*.tmp' -print
# delete
find ./tmpdir -type f -name '*.tmp' -deleteOne filesystem
sudo rm -r --one-file-system /mnt/usb/dataAvoids walking into bind mounts you did not intend.
Notes & Pitfalls
- Globs are expanded by the shell before
rmruns.
- Hidden files:
rm *does not remove dotfiles;rm -r dirdoes remove them insidedir.
- Busy files may unlink but space frees when last fd closes (
lsofdeleted files).
- ZFS/btrfs snapshots and backups are real recovery options — not
rmitself.
Additional Resources
man rm