ch01/l01

Absolute, relative, and remembered paths

Use path forms intentionally instead of wandering the tree.

cd - 20 min read, 20 min lab beginner

An absolute path starts at `/`. A relative path starts from the current directory. `.` is here, `..` is parent, `~` is home, and `cd -` returns to the previous directory. These are navigation controls, not trivia.

In the field

You need to compare a config under `/etc` with logs under `/var/log` without losing your working location.

Worked command

$ pwd/home/studio-dev$ cd /etc/ssh$ cd /var/log$ cd -/etc/ssh$ realpath ../systemd
Anti-pattern

Do not use a relative path when you are not certain of the current directory.

Safer pattern

Use `pwd` before broad commands and absolute paths for high-consequence targets.

Knowledge check

You run a destructive command against `../tmp` instead of `/tmp`. Compared to `/tmp`, what makes the relative form riskier?

  • A It always points one level above your home directory
  • B Its target depends on your current directory, which you may have wrong
  • C It cannot be expanded by the shell, so it silently does nothing
  • D It resolves to the root filesystem when the parent is missing
Show the answer

Correct: B. Its target depends on your current directory, which you may have wrong

Why

`../tmp` resolves against whatever directory you currently stand in, so a wrong assumption about the cwd aims the command at the wrong target; `/tmp` is fixed no matter where you are. The relative form does not 'always' sit above home — that holds only for one specific cwd.

Practice checklist

  1. Create `/tmp/td-nav/a/b/c`.
  2. Navigate using one absolute path and two relative paths.
  3. Use `cd -` to return to the previous directory.

Deliverable evidence

  • A command transcript showing absolute and relative movement.
  • One sentence explaining `.` and `..`.
Teaching diagramch01 · mental model
Every path resolves to one absolute location cwd pwd shows it . here .. parent ~ home /abs/target realpath proves it find /var/log -type f -name '*.log' -mtime -1 walks the tree, filters to evidence absolute path = same target from anywhere | relative path = depends on cwd

shows: How `.`, `..`, and `~` resolve from the current directory into a single absolute location, with `pwd`/`realpath` confirming where you actually are and `find` filtering a tree down to evidence.

does not prove: It shows how path tokens resolve in principle; it does not prove your shell's current directory at any moment — only running `pwd` or `realpath` on the live machine proves that.

Memorize this

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

cdcd -...~
Recall practice · Scenario -> next check

cue You changed directories twice and want to jump straight back to the directory you were in before this one.

show recall target

cd -