Course outline · 0% complete

0/29 lessons0%

Course overview →

Paths: the address of every file

lesson 2-1 · ~9 min · 4/29

Quiz

Warm-up from lesson 1-2: what does pwd print?

Absolute vs relative paths

Everything on Linux names files by path: error messages (No such file or directory), configuration files, scripts, and every command that takes a file argument. A large share of beginner debugging ends with "the command was given the wrong path", so learn the addressing rules once, properly.

A path is the address of a file or directory. There are exactly two kinds:

  • Absolute path: starts with / (the root) and spells out the whole route, like /home/sam/projects/notes.txt. It means the same thing no matter where you are standing.
  • Relative path: starts from your current working directory, like projects/notes.txt. Its meaning changes as you move around.

Two special names work inside any path:

  • . means the current directory
  • .. means the parent directory (one level up)

So cd .. moves up one level, and cd ../.. moves up two.

/home/tmp/etc/sam/projects/notes.txtapp.pyabsolute: /home/sam/projects/app.pyfrom sam/: projects/app.pyfrom projects/: ../notes.txt
One tree, three ways to address things. The gold directory (sam/) is where you're standing for the relative examples.

Code exercise · bash

Run it. We build a nested tree, dive in with one relative path, then climb out with .. The `basename` command shows just the last part of pwd's answer, so you can see each hop.

Quiz

Which of these is a relative path?

Code exercise · bash

Your turn. Create the nested directory `garden/roses` in one command, cd into it, print its name with `basename "$(pwd)"`, then go up one level with .. and print that directory's name too. Expected output: ``` roses garden ```