rm: Remove Files
rm (remove) deletes files, and with -r whole directory trees. There is no recycle bin on the command line: what rm deletes is gone. This lesson shows how to use it effectively and, just as important, safely.
Removing files
touch draft1.txt draft2.txt final.txt
rm draft1.txt
ls
rm draft2.txt final.txt
ls | wc -ldraft2.txt
final.txt
0
rm accepts many files at once and prints nothing when it succeeds. Removing something that doesn't exist is an error, unless you add -f:
rm ghost.txt
echo "status: $?"
rm -f ghost.txt
echo "status with -f: $?"rm: cannot remove 'ghost.txt': No such file or directory
status: 1
status with -f: 0
Options
| Option | Effect |
|---|---|
-i |
Ask before every removal (interactive) |
-I |
Ask once before removing more than three files or recursing |
-f |
Force: never ask, ignore missing files |
-r / -R |
Recursive: remove directories and their contents |
-v |
Verbose: report each removal |
-d |
Remove empty directories (like rmdir) |
Removing directories
Plain rm refuses to delete a directory. rm -r deletes it together with everything inside:
mkdir -p build/assets/img
touch build/index.html build/assets/app.js build/assets/img/logo.png
rm build
rm -rv build
lsrm: cannot remove 'build': Is a directory
removed 'build/index.html'
removed 'build/assets/app.js'
removed 'build/assets/img/logo.png'
removed directory 'build/assets/img'
removed directory 'build/assets'
removed directory 'build'
The -v output shows that rm -r works bottom-up: files first, then each directory once it's empty.
Wildcards: check before you delete
rm is often combined with globs like *.log. The shell expands the pattern into file names before rm runs, so you can preview exactly what would be deleted with echo or ls:
touch app.log app.log.1 error.log notes.txt report.pdf
echo "Would delete:" *.log
rm *.log
lsWould delete: app.log error.log
app.log.1
notes.txt
report.pdf
*.log matches names ending in .log, so app.log.1 survives. Previewing with echo costs nothing and has saved many people from disaster.
Safety rules
Warning:
rm -rfwith a wrong path is one of the most destructive things you can type. Keep these habits:
- Look before you leap:
ls patternfirst, thenrm pattern. - Avoid
rm -rfon variables you didn't check. If$diris empty,rm -rf "$dir"/*becomesrm -rf /*. Use"${dir:?}", which aborts when the variable is empty or unset. - Don't run as root unless you must. GNU
rmalso refusesrm -rf /without--no-preserve-root. - Use
-ior-Iwhen deleting interactively by pattern. - Back up anything you can't recreate.
cleanup() {
local target="$1"
rm -rf "${target:?refusing to delete an empty path}"/cache
echo "cleaned $target/cache"
}
mkdir -p myapp/cache && touch myapp/cache/tmp1
cleanup myapp
( cleanup "" )
echo "subshell exit status: $?"cleaned myapp/cache
/w/main.sh: line 3: target: refusing to delete an empty path
subshell exit status: 1
The empty call stops with an error message instead of deleting /cache. In a script, a failed ${var:?} ends the whole script, which is exactly what you want before a dangerous command. Here the call ran inside ( ... ), a subshell, so only the subshell ended and we could print its failure status.
Files with awkward names
A file whose name starts with - looks like an option. Use -- (end of options) or a ./ prefix:
touch -- -verbose.txt "monthly report.txt"
rm -verbose.txt
rm -- -verbose.txt
rm "monthly report.txt"
ls -A | grep -v gitconfig | wc -lrm: invalid option -- 'e'
Try 'rm ./-verbose.txt' to remove the file '-verbose.txt'.
Try 'rm --help' for more information.
0
GNU rm even suggests the fix in its error message. The -- convention works for almost every command, not just rm. Names with spaces just need quotes.
Moving to a trash folder instead
On desktops, tools like gio trash or trash-put move files to the desktop recycle bin. In scripts, a simple alternative is to move files to a dated "trash" folder with mv and delete it later, when you're sure.
Test yourself
Which command deletes the directory old and everything in it?
- rm old
- rmdir old
- rm -r old
- rm -i old
How can you safely see which files rm *.tmp would delete?
- rm -n *.tmp
- echo *.tmp (or ls *.tmp)
- rm --dry *.tmp
- You can't
How do you delete a file named -f?
- rm -f
- rm "-f"
- rm -- -f
- rm \-f