ch02/l02

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.

In the field

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
Anti-pattern

Do not run `rm` from an unverified directory or with an unpreviewed broad glob.

Safer pattern

Use `pwd`, preview with `ls` or `printf`, then remove with the narrowest path.

Knowledge check

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?

  • A rm scanned the directory and matched *.bak itself
  • B The shell expanded *.bak into filenames before rm ran
  • C rm -i re-globs the pattern as it prompts for each file
  • D The filesystem resolved the wildcard when rm opened it
Show the answer

Correct: B. The shell expanded *.bak into filenames before rm ran

Why

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

  1. Create three `.bak` files in a disposable directory.
  2. Preview the glob matches.
  3. Delete interactively and confirm only those files changed.

Deliverable evidence

  • A transcript showing `pwd`, preview, delete, and final listing.
Teaching diagramch02 · mental model
Preview-first discipline: prove the target before you change it locate pwd preview ls / printf / stat act cp -i / mv / rm -i verify ls/stat shell expands the glob FIRST *.bak -> file1 file2 file3 rm only ever sees the final names read before you dump less / head -n / tail -f for bounded reads grep -rn gives file:line evidence never act from an unverified directory or an unpreviewed glob

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.

Memorize this

Commit these to memory, then drill them until recall is automatic.

mvrm -iglobs expand firstpwd before rm
Recall practice · Output -> explanation

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