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.
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
Do not overwrite a file before you know whether permissions, owner, or timestamp matter.
Make a backup and inspect it with `stat` before changing the original.
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?
Show the answer
Correct: B. cp -a app.conf backup.conf
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
- Create a lab tree.
- Copy a file with interactive overwrite protection.
- Compare metadata with `stat`.
Deliverable evidence
- Before/after `stat` output.
- A note describing why you chose the copy flags.
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 Copy a config so permissions, owner, and timestamps survive for a faithful restore.
show recall target
cp -a