Course outline · 0% complete

0/29 lessons0%

Course overview →

Paths: the address of every file

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

Recall from lesson 1-2 that pwd prints the full address of the directory you are standing in, something like /home/sam/projects. Listing the contents of that directory is a separate job, handled by ls.

Both stay useful here, because this lesson is about reading and writing those addresses.

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.

Diving in with a relative path, then climbing back out

This example builds a nested tree, descends into it with a single relative path, then climbs out using ... The basename command trims an address down to its last component, which makes each hop easy to see.

mkdir -p town/library/books
cd town/library/books
basename "$(pwd)"
cd ..
basename "$(pwd)"
cd ../..
echo "back where we started"

Output

books
library
back where we started

Two details carry the example. mkdir -p creates the whole chain of directories in one shot rather than requiring three separate commands, which lesson 2-3 covers in detail. And cd town/library/books is a single relative path performing three levels of descent, which is exactly what relative paths are for.

Of /home/ada, /etc/hosts, /usr/bin, and projects/app.py, the relative path is projects/app.py. The rule is purely mechanical: any path that does not begin with / is relative, and it gets resolved starting from wherever you are currently standing.

The other three all begin at the root, which makes them absolute. /home/ada, /etc/hosts, and /usr/bin refer to the same three locations from any working directory, while projects/app.py means something different depending on where the shell happens to be.

The same pattern works for any nested pair of directories. mkdir -p garden/roses creates both levels at once, cd garden/roses descends in one hop using a relative path, and basename "$(pwd)" prints just the name of the directory you are in rather than its whole address.

mkdir -p garden/roses
cd garden/roses
basename "$(pwd)"
cd ..
basename "$(pwd)"

Output

roses
garden

The two basename lines bracket the movement, so the output records the trip. cd .. moves to the parent directory, taking the shell from roses up to garden, which is why the second line of output is the outer directory's name.