Course outline · 0% complete

0/29 lessons0%

Course overview →

Deleting safely: rm

lesson 3-3 · ~9 min · 9/29

rm is forever

rm file removes a file. Burn this in now: there is no trash can. The file does not move somewhere recoverable, it is simply gone. That is the real reason lesson 2-2 pushed tab completion so hard, since completing a filename means you delete the file you meant instead of a typo-neighbor.

The family of deletion commands:

CommandWhat it deletesNotes
rm file.txtOne fileNo confirmation, no undo
rm -i file.txtOne filePrompts before each deletion (i for interactive)
rmdir emptydirOne directoryOnly succeeds if the directory is already empty
rm -r dirA directory and everything in itr for recursive
rm -rf dirA directory and everything in it-f (force) skips every confirmation and warning

Treat rm -rf like a chainsaw. Never run it on a path you have not just verified with ls, and never on /.

Deleting a file, then a whole tree

The first rm removes a single file, and the ls that follows proves keep.txt survived. The second half builds a small directory tree and removes it with rm -r.

touch old.txt keep.txt
rm old.txt
ls
mkdir -p junk/deep
touch junk/deep/temp.txt
rm -r junk
echo "junk is gone"
ls

Output

keep.txt
junk is gone
keep.txt

The two keep.txt lines are the two ls calls, and the fact that they are identical is the point: nothing except the intended targets was affected. Plain rm would have refused the junk directory outright, which is precisely the gap -r fills.

The flag that makes rm ask for confirmation before each file is -i, short for interactive. It prompts for a y or n answer per file, which is a good habit to keep while the commands are still unfamiliar.

The neighboring flags do unrelated things. -r recurses into directories, controlling what gets deleted rather than whether you are asked. -f is the opposite of careful: it forces deletion and suppresses the very prompts that -i adds.

A pre-delete checklist

Before any rm -r, professionals do a two-second ritual:

  1. ls the-target to see exactly what's about to disappear.
  2. Read the command back once. Especially check for stray spaces: rm -r stuff / deletes stuff and then starts on the root /, while rm -r stuff/ deletes only stuff.

That one space has destroyed real production servers. The habit costs nothing.

The safe-delete ritual, step by step

This session walks through removing a directory called old-project the careful way, showing each command with the output it produced.

Nothing gets deleted until its contents have been seen, so the first command is a listing rather than a removal:

$ ls old-project
cache.tmp
draft.bak

A cache file and a stale backup, with nothing worth keeping, so the directory can go. rm -r removes it along with everything inside, and rm -rf old-project would do the same while additionally suppressing any prompts:

$ rm -r old-project

Like most successful Unix commands it printed nothing at all, so a final listing confirms the directory is gone and that the neighbors survived:

$ ls
docs
src

rm -r old-project deletes the directory together with its entire contents. rm -rf old-project does the same job while suppressing prompts, but the reflex worth building is running ls old-project first, because rm has no undo.

The recursive flag is not optional here. rm on its own refuses to touch a directory at all, so removing a tree requires -r to authorize descending into it and deleting what it finds.