Move and remove after preview
Treat deletion as a workflow, not a single command.
rm -i
20 min read, 25 min lab
beginner
`mv` can rename or relocate. `rm` deletes names from the filesystem. Globs expand before the command runs, so preview the matches before deletion. In real operations, narrow the directory, list the target, then remove.
A cleanup task says remove old `.bak` files. You need to prove the match set before the delete.
Worked command
$ pwd/tmp/td-files$ ls -la ./*.bak$ printf '%s\n' ./*.bak$ rm -i ./*.bak
Do not run `rm` from an unverified directory or with an unpreviewed broad glob.
Use `pwd`, preview with `ls` or `printf`, then remove with the narrowest path.
You run `rm -i *.bak` and it offers to delete a file you did not expect. What actually decided which files rm was asked to remove?
Show the answer
Correct: B. The shell expanded *.bak into filenames before rm ran
The shell expands the glob into a literal list of names before rm starts, so rm only ever sees final filenames, never the pattern. That is why you preview with ls or printf using the same glob first; rm -i only confirms the already-expanded set, it never re-matches.
Practice checklist
- Create three `.bak` files in a disposable directory.
- Preview the glob matches.
- Delete interactively and confirm only those files changed.
Deliverable evidence
- A transcript showing `pwd`, preview, delete, and final listing.
shows: The repeated locate -> preview -> act -> verify loop behind every file change, plus the fact that the shell expands globs before the command ever runs and that bounded readers precede searching.
does not prove: The loop is a discipline, not a guarantee: a clean preview proves the target you saw, not that another process won't change the directory between preview and act, nor that flags like -a copied the right metadata.
Commit these to memory, then drill them until recall is automatic.
cue Who turns *.bak into actual filenames, and when, relative to rm running?
show recall target
the shell expands the glob first, before rm runs