Skip to content
Courses

Navigating the Linux Filesystem

Linux FoundationsLesson 3 of 945 minutesLessonBuilds on: Shell, Identity, and ExpansionNo lab next

The question: what will this path name from here?

Section titled “The question: what will this path name from here?”

The same relative path can point at completely different things depending on where you happen to be standing.

Linux lays out files and directories as a single hierarchy. At the very top sits the root directory, written /. Every ordinary absolute path starts there and works its way down through a sequence of names. Your home directory is one branch of that tree assigned to your user. The shell also remembers a current working directory, and that’s the starting point every relative path is measured from.

So think of a path as directions rather than as a name. /home/student/notes.txt says “start at root, go into home, go into student, then look for notes.txt.” Plain notes.txt says “look for that name right here.” The street-directions analogy is useful, and it has one limit worth knowing early. .. isn’t text substitution. The system resolves each component against the directory it has reached so far.

So let’s start by working out where you currently are:

Terminal window
pwd

Your username will be different, but the result should have this shape:

/home/student

A leading / means the path starts at the filesystem root, ~ is shell shorthand for your home directory, . means the current directory, and .. means the parent of the current directory.

The shell expands ~ before the command sees the argument. The filesystem uses / to separate path components and gives . and .. their current-directory and parent-directory meanings during path resolution. That separation matters quite a bit. An error can come from shell expansion, from the starting directory, or from a component that simply isn’t there (three different problems, three different fixes).

Run this setup exactly:

Terminal window
mkdir -p ~/underdunn-course/exercises/navigation/evidence/raw
mkdir -p ~/underdunn-course/exercises/navigation/reports
touch ~/underdunn-course/exercises/navigation/evidence/notes.txt
touch ~/underdunn-course/exercises/navigation/reports/summary.txt
cd ~/underdunn-course/exercises/navigation

Verify the starting state:

Terminal window
pwd
ls

Expected evidence, with your username substituted in the path:

/home/student/underdunn-course/exercises/navigation
evidence reports

The real directory tree is:

navigation/
├── evidence/
│ ├── notes.txt
│ └── raw/
└── reports/
└── summary.txt

From navigation, predict the pwd result before each move. Write them all down first, because a prediction made after the output appears isn’t really a prediction:

Terminal window
cd evidence
pwd
cd raw
pwd
cd ../..
pwd

That last command moves from raw up to evidence, and then from evidence up to navigation.

So the observation is that .. gets interpreted from wherever you are at that exact moment. And the conclusion is narrower than it sounds. A relative path is only half a location, and it needs a known starting directory to mean anything.

Each .. is resolved against the directory reached so far, not pasted into the path text
navigation/evidence/rawwhere cd ../.. starts
navigation/evidenceafter the first ..
navigationafter the second ..

Linux resolves a path from left to right, one piece at a time. That’s why a wrong component fails where it sits, instead of quietly resolving to something else further along. When a path fails, check its starting directory and each component in order.

Return to the stable start:

Terminal window
cd ~/underdunn-course/exercises/navigation

These commands name the same directory:

Terminal window
ls evidence
ls ~/underdunn-course/exercises/navigation/evidence

The first is relative to wherever you currently are. The second expands ~ and then follows a path anchored in your home directory, so it means the same thing from anywhere.

Change one variable by moving to reports:

Terminal window
cd reports

Predict which of those two commands still works unchanged, and then test both. The home-based path still names the same location. The relative evidence now looks inside reports instead, and should fail.

Compare that error with the earlier successful command. The path text never changed; only your starting location did.

Coreutils directory operations documents the listing options used here. Compare a listing with pwd before deciding that a file disappeared.

Return to the stable start and create two names:

Terminal window
cd ~/underdunn-course/exercises/navigation
touch "field notes.txt" .checkpoint
ls
ls -a

The ordinary listing shows field notes.txt but not .checkpoint. The -a listing shows both. The quotes kept the space inside one argument, and the quote characters themselves are not part of the filename.

A leading dot doesn’t give a file some special hidden attribute (this surprises people coming from other systems). Most Unix tools usually leave dot-prefixed names out of an ordinary listing, and -a is what asks ls to include them anyway. The entry exists either way, and you can always name it directly, whether or not the default listing bothers to show it.

Predict the argument you’d need to list only the spaced name, and then check:

Terminal window
ls "field notes.txt"

Starting from navigation/reports:

  1. Predict a relative path to evidence/notes.txt.
  2. List that file without leaving reports.
  3. Move to evidence/raw using a relative path.
  4. Print the absolute current directory.
  5. List the parent directory without leaving raw.

Check each prediction and each result before you move on to the next step.

  • No such file or directory: run pwd first, then read the relative path again starting from what it printed.
  • A path with spaces produces two errors instead of one: quote the whole path, not just part of it.
  • Names seem to be missing: use ls -a whenever the question involves entries that begin with a ..
  • You’ve completely lost track of where you are: get back with cd ~/underdunn-course/exercises/navigation and confirm it with pwd. This happens to everybody.
  • The practice tree is wrecked: confirm you’re inside ~/underdunn-course/exercises, remove only the navigation directory, and run the setup block again. I’d recommend checking pwd twice before you remove anything.

Compare pwd after the absolute and relative moves. The absolute path starts at /; the relative path starts in your current directory. If you arrived somewhere unexpected, walk through each .. from that starting directory.