rm
Deleting files and directories — no trash, no undo
rm removes file names. There is no trash: what's deleted can't be restored. The data is freed when the last name (hard link) disappears and nobody has the file open. For empty directories there is rmdir. The sandbox has GNU coreutils 9.4.
Syntax
rm [options] file…
rm -r [options] dir…
rmdir [options] empty_dir…
Exit code: 0 — everything removed, 1 — something failed (no file, no permission, a directory without -r).
rm options
| Option | What it does |
|---|---|
-r, -R | remove directories together with all their contents |
-d | remove empty directories (like rmdir) |
-f | don't ask and don't complain about nonexistent files; exit code 0 |
-i | ask about every file |
-I | ask once if there are more than three files or -r is given; a soft safeguard against rm * |
--interactive=never|once|always | the same, long form: never ≈ no questions, once = -I, always = -i |
-v | print each removal: removed 'd/f', removed directory 'd' |
--one-file-system | with -r, don't descend into other mounted file systems |
--preserve-root | refuse to remove / (on by default) |
-- | end of options: everything after it is a file name |
Of -f, -i, -I, the last one given wins.
rmdir options
| Option | What it does |
|---|---|
-p | remove the directory and then its empty parents: rmdir -p a/b/c removes c, b, a |
-v | print each removal |
--ignore-fail-on-non-empty | silently skip non-empty directories |
rmdir removes only empty directories — and that is its advantage: you can't accidentally wipe data with it.
When rm asks on its own
Without -i, rm asks remove write-protected regular file? only if:
- the file has no write permission for you (root never runs into these questions — it can always write),
- and standard input is a terminal. In a script or a pipeline there is no question, the file is removed.
Permissions on the file itself don't matter for removal: you need write and execute (w and x) permissions on the directory it lives in. In a directory with the sticky bit (/tmp) you can't remove other users' files.
Examples
# remove a file
rm old.log
# a directory with all its contents, with a report
rm -rv build/
# no error if the file doesn't exist (handy in scripts)
rm -f /tmp/app.lock
# one question before a mass removal
rm -I *.tmp
# remove a file whose name starts with a dash
rm -- -file.txt
rm ./-file.txt
# an empty directory and its empty parents
rmdir -p cache/2024/01
# all .bak files in a tree — safe for names with spaces
find . -name '*.bak' -type f -delete
find . -name '*.bak' -print0 | xargs -0 rm -f --
# first look at what the pattern will match
echo rm -r /srv/app/releases/old-*
Pitfalls
- There is no trash. Before
rm -rfwith a variable or a pattern, first run the same withechoorls. - An empty variable.
rm -rf "$DIR/"*with an empty$DIRbecomesrm -rf /*. Protection:rm -rf "${DIR:?not set}"/*— with an empty variable the shell stops with an error. - A name starting with a dash:
rm -x→ "invalid option -- 'x'". Writerm -- -xorrm ./-x. rm -fhides typos: a nonexistent path gives neither an error nor a non-zero exit code.- A space in a path:
rm -rf /srv/app /oldremoves two directories —/srv/appand/old. Quote paths with variables. - Space isn't freed if a process holds the file open: the name is gone, the data stays until it's closed. Such files show up in
/proc/*/fdas(deleted). For live logs usetruncate -s 0instead ofrm. rm -rf /refuses to run without--no-preserve-root, butrm -rf /*doesn't: the shell expands the pattern.rm -r .andrm -r ..are forbidden: "refusing to remove '.' or '..' directory".