ch02/l01

Create and copy without losing metadata

Build test spaces, copy intentionally, and preserve metadata when it matters.

mkdir -p 20 min read, 25 min lab beginner

`mkdir -p` creates parent directories without failing if they already exist. `touch` creates files or updates timestamps. `cp -a` preserves recursive structure and metadata; `cp -i` asks before overwrite.

In the field

You need a disposable lab tree and a backup copy of a config before editing.

Worked command

$ mkdir -p /tmp/td-files/{input,backup,output}$ touch /tmp/td-files/input/app.conf$ cp -ip /tmp/td-files/input/app.conf /tmp/td-files/backup/app.conf
Anti-pattern

Do not overwrite a file before you know whether permissions, owner, or timestamp matter.

Safer pattern

Make a backup and inspect it with `stat` before changing the original.

Knowledge check

You back up a config so you can restore it byte-for-byte if an edit goes wrong. Which copy preserves ownership, permissions, and timestamps?

  • A cp app.conf backup.conf
  • B cp -a app.conf backup.conf
  • C cp -i app.conf backup.conf
  • D touch backup.conf then cp app.conf backup.conf
Show the answer

Correct: B. cp -a app.conf backup.conf

Why

cp -a is archive mode: it preserves mode, owner, and timestamps and copies recursively. Plain cp and cp -i write a new file with current time and your default ownership; -i only adds an overwrite prompt, it does not preserve metadata.

Practice checklist

  1. Create a lab tree.
  2. Copy a file with interactive overwrite protection.
  3. Compare metadata with `stat`.

Deliverable evidence

  • Before/after `stat` output.
  • A note describing why you chose the copy flags.
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.

mkdir -ptouchcp -icp -astat
Recall practice · Meaning -> command

cue Copy a config so permissions, owner, and timestamps survive for a faithful restore.

show recall target

cp -a