TExcelTAD
Home › Linux & Bash › rm: Remove Files

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

bash · try it yourself
touch draft1.txt draft2.txt final.txt
rm draft1.txt
ls
rm draft2.txt final.txt
ls | wc -l
draft2.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:

bash · try it yourself
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:

bash · try it yourself
mkdir -p build/assets/img
touch build/index.html build/assets/app.js build/assets/img/logo.png
rm build
rm -rv build
ls
rm: 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:

bash · try it yourself
touch app.log app.log.1 error.log notes.txt report.pdf
echo "Would delete:" *.log
rm *.log
ls
Would 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 -rf with a wrong path is one of the most destructive things you can type. Keep these habits:

  1. Look before you leap: ls pattern first, then rm pattern.
  2. Avoid rm -rf on variables you didn't check. If $dir is empty, rm -rf "$dir"/* becomes rm -rf /*. Use "${dir:?}", which aborts when the variable is empty or unset.
  3. Don't run as root unless you must. GNU rm also refuses rm -rf / without --no-preserve-root.
  4. Use -i or -I when deleting interactively by pattern.
  5. Back up anything you can't recreate.
bash · try it yourself
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:

bash · try it yourself
touch -- -verbose.txt "monthly report.txt"
rm -verbose.txt
rm -- -verbose.txt
rm "monthly report.txt"
ls -A | grep -v gitconfig | wc -l
rm: 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?

  1. rm old
  2. rmdir old
  3. rm -r old
  4. rm -i old

How can you safely see which files rm *.tmp would delete?

  1. rm -n *.tmp
  2. echo *.tmp (or ls *.tmp)
  3. rm --dry *.tmp
  4. You can't

How do you delete a file named -f?

  1. rm -f
  2. rm "-f"
  3. rm -- -f
  4. rm \-f