← Full reference

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

OptionWhat it does
-r, -Rremove directories together with all their contents
-dremove empty directories (like rmdir)
-fdon't ask and don't complain about nonexistent files; exit code 0
-iask about every file
-Iask once if there are more than three files or -r is given; a soft safeguard against rm *
--interactive=never|once|alwaysthe same, long form: never ≈ no questions, once = -I, always = -i
-vprint each removal: removed 'd/f', removed directory 'd'
--one-file-systemwith -r, don't descend into other mounted file systems
--preserve-rootrefuse 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

OptionWhat it does
-premove the directory and then its empty parents: rmdir -p a/b/c removes c, b, a
-vprint each removal
--ignore-fail-on-non-emptysilently 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 -rf with a variable or a pattern, first run the same with echo or ls.
  • An empty variable. rm -rf "$DIR/"* with an empty $DIR becomes rm -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'". Write rm -- -x or rm ./-x.
  • rm -f hides typos: a nonexistent path gives neither an error nor a non-zero exit code.
  • A space in a path: rm -rf /srv/app /old removes two directories — /srv/app and /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/*/fd as (deleted). For live logs use truncate -s 0 instead of rm.
  • rm -rf / refuses to run without --no-preserve-root, but rm -rf /* doesn't: the shell expands the pattern.
  • rm -r . and rm -r .. are forbidden: "refusing to remove '.' or '..' directory".

See also

find · mv · ln · truncate · chmod · mkdir · proc