Terminal · foundation
Files and directories
Files store byte sequences and directories map names to filesystem objects; command-line utilities create, inspect, copy, rename, and remove those entries.
Why it matters
Most terminal work changes persistent filesystem state, where misunderstanding copy, move, or removal semantics can lose data.
Mental model
How to reason about files and directories
Treat a directory as a table of names rather than a physical box; operations update directory entries, file contents, or both depending on the command and filesystem.
Analogy
A directory resembles a library catalog that maps titles to items, while a regular file is one of the items whose contents can be read or replaced.
Examples
See the boundary, not just the happy path
Worked example · Create and copy
mkdir -p backup && cp -- report.txt backup/report.txtmkdir -p establishes the destination directory, and -- prevents a dash-prefixed source name from being parsed as an option by compatible utilities.
Worked example · Rename within a filesystem
mv draft.txt final.txtWhen source and destination are on the same filesystem, mv normally renames a directory entry rather than copying file bytes and then deleting the source.
Avoid · Unreviewed recursive removal
rm -rf "$target"Quoting does not make an empty, root-level, or incorrectly computed target safe; destructive recursive operations require explicit target validation.
Common mistakes
Misconceptions to remove early
Assuming rm provides an undo operation
Command-line removal commonly bypasses a desktop trash mechanism, so important data needs backups or an intentionally recoverable workflow.
Confusing a directory entry with file contents
Renaming a file usually changes its directory entry, while copying creates another file whose later content changes are independent.
Quick check
Can you predict the result?
1. What is the usual effect of cp a.txt b.txt?
- • It creates or replaces b.txt with a copy whose later content changes are independent of a.txt.
- • It creates a symbolic link named b.txt.
- • It renames a.txt to b.txt.
2. Why should a script validate a recursive removal target even when the variable is quoted?
Keep building