Check a pattern before deleting
The shell expands the pattern (see globbing), so ls with the same pattern shows
exactly the list rm will get.
The rm command deletes files and directories at once, with no trash; -i and -I ask first, -r removes a tree.
rm notes.txt
delete a file
rm -i *.log
ask about every file
rm -I *.log
ask once if there are more than three
rm -r old-project
a directory with everything inside
rm -rf build/
no prompts, no errors about missing files
rm -d empty-dir
an empty directory (like rmdir)
rm -v *.tmp
print each removal
rm -- -file.txt
a name that starts with a dash
ls *.log && rm -i *.log
look first, then delete
rm (remove) deletes file names from a directory. There's no trash in the terminal:
a deleted file isn't kept anywhere, and there's no "undo" command.
rm deletes a directory only with -r (recursively, with its contents) or, if it's
empty, with -d. -f (force) drops prompts and errors about missing files: rm -rf
silently deletes everything it can.
Deleting needs write permission on the directory that holds the file. The file's own
permissions decide only one thing: whether rm asks before removing a write-protected
file.
Full list — man rm. In practice -i, -I, -r, -f, -v are enough.
| Option | What it does | Example |
|---|---|---|
-r |
Remove a directory with everything inside (same as -R). | rm -r old-project |
-f |
Never prompt and stay silent about missing files. dangerous | rm -rf build/ |
-i |
Prompt before every removal. | rm -i *.log |
-I |
Prompt once — when removing more than three files or recursively. | rm -I *.log |
-v |
Print each removal — removed 'x'. | rm -v *.tmp |
-d |
Remove an empty directory. | rm -d empty-dir |
--one-file-system |
When removing recursively, skip directories on other mounted filesystems. | rm -r --one-file-system /mnt/old |
Nothing found. Try a shorter query.
The shell expands the pattern (see globbing), so ls with the same pattern shows
exactly the list rm will get.
rm can't select by date or size — that's find's job.
rm -rf against an empty variable${VAR:?} stops the command if the variable is empty or unset.
The argument is a directory. rm -d removes an empty one, rm -r one with contents
(look inside first: ls -R dir).
You can't write to the directory holding the file. Check ls -ld $(dirname path).
System files are removed with sudo rm, but first make sure no package or service
needs the file.
The file lacks w. rm asks instead of refusing: answer y, or delete with -f if
you're sure.
The file has the immutable attribute (lsattr file shows i), or it's someone else's
file in a sticky directory like /tmp. Clear the attribute with sudo chattr -i
file.
A process still has the file open: the name is gone, the data isn't. Find the holder
with sudo lsof +L1 — see lsof and disk-usage. The space comes back when
that process restarts.
The pattern matched too many files for one command. Delete with find: find .
-name '*.tmp' -delete.
rm doesn't move files to a trash. If a file isn't in a backup or in git, consider
it gone. Before a mass delete run ls with the same pattern, or use rm -I.
rm -rf with a stray space or an empty variablerm -rf / tmp/build (an extra space) asks to delete the root. A built-in check
guards / (it is dangerous to operate recursively on '/'), but /*, ~ and .
have no guard: rm -rf "$DIR"/* with an empty $DIR becomes rm -rf /*. Write
"${DIR:?}" and read the whole command before pressing Enter.
Found an inaccuracy? Report an error in the article