online posts 16
linux and the command line 101

task 1.2 · chapter 1 of 8: the shell and the filesystem · 15 min

Paths

objective

Find a config file in a small tree of sites and racks, write its absolute path, then reach it with relative paths from two other folders, using .. to climb up.

you edit, in the labanswers/1.2.yml

open a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter

given
LessonThe shell and the filesystem
The treefiles/site/: two sites, two racks each, one leaf switch’s config per rack
Your answersanswers/1.2.yml
new to this? step by stepbeginner
  1. cd files/site, then ls, cd into folders, cd .. to come back up, and pwd whenever you’re lost.
  2. Find chi-leaf2.cfg. Its absolute path is pwd plus its name.
  3. cd files/site/nyc/rack1. Which path, with .., gets from there to the file? Try it with cat.
  4. Fill in answers/1.2.yml, then make check T=1.2.

more in meet the lab

1 explain

Two ways to say where a file is

Absolute and relative

The same file has many names, depending on where you start.

  • An absolute path starts at / and lists every folder down to the file. It means the same thing wherever you are, which is why scripts and config files use them.
  • A relative path starts from your working directory, the folder pwd prints. It’s shorter, and it’s what you type most of the time.

Two names help you move around in relative paths:

NameMeans
.this folder
..the folder above this one. ../.. is two folders up
~your home folder (not relative: it always means the same place)

in one sentence Absolute paths start at /. Relative paths start where you are, and .. climbs one folder up.

Tab does the typing

Press Tab after the first few letters of a file or folder name, and the shell finishes it for you. Press it twice when nothing happens, and it lists what could come next. It’s faster, and it never makes a typo, so paths you build with Tab are paths that exist.

2 show

Walking the tree

ls -R lists a folder and everything under it (-R is recursive):

$ ls -R files/site
files/site:
chi  nyc

files/site/chi:
README.txt  rack1  rack2

files/site/chi/rack1:
chi-leaf1.cfg

files/site/chi/rack2:
chi-leaf2.cfg
...

From files/site/nyc/rack2, one folder up is files/site/nyc, so ../README.txt is nyc’s README:

$ cd files/site/nyc/rack2
$ cat ../README.txt
The nyc site: one leaf switch in each rack.
3 follow along

Up and down the tree

  1. See the whole tree at once:

    run in the terminal
    ls -R files/site
    
  2. Walk down into one rack. Type cd files/si, press Tab, then n and Tab again, and watch the shell fill the names in:

    run in the terminal
    cd files/site/nyc/rack2
    pwd
    ls
    
  3. Read a file one folder up, without leaving:

    run in the terminal
    cat ../README.txt
    
  4. Climb back up, one folder at a time, and check where you are each time:

    run in the terminal
    cd ..
    pwd
    cd ../../..
    pwd
    

    You’re back in the lab’s folder.

in one sentence .. is the folder above; chain them (../..) to climb further, and Tab to type names you can trust.

4 challenge

Find chi-leaf2

Fill in answers/1.2.yml:

  1. chi_leaf2_absolute: the absolute path of chi-leaf2.cfg, wherever it is under files/site.
  2. from_nyc_rack1: a relative path to that same file, starting from files/site/nyc/rack1. Check it: cd into that folder and cat your path. If it prints the config, it’s right.
  3. readme_from_chi_rack2: a relative path to files/site/chi/README.txt, starting from files/site/chi/rack2.
  4. parent_of_home: the absolute path of the folder your home folder is in.

Then come back to the lab’s folder (cd /workspaces/linux-cli-101, or cd -) and run make check T=1.2.

hint

For the absolute path, cd into the folder that holds the file and run pwd. Add /chi-leaf2.cfg to what it prints.

hint

From nyc/rack1, how many folders up do you have to go before chi is in sight? Each one is a ...

hint

Your home folder is /home/vscode. Its parent is one step up the same path.

show the solution
chi_leaf2_absolute: /workspaces/linux-cli-101/files/site/chi/rack2/chi-leaf2.cfg
from_nyc_rack1: ../../chi/rack2/chi-leaf2.cfg
readme_from_chi_rack2: ../README.txt
parent_of_home: /home

From files/site/nyc/rack1, the first .. is files/site/nyc and the second is files/site, where chi lives. On a laptop, the absolute path starts wherever you cloned the lab.

5 check: run it, then prove it

$ make check T=1.2

when it's right, you'll see

╭─ task 1.2 · paths
│  ✓ the absolute path to chi-leaf2.cfg (challenge): ...
│  ✓ a relative path to chi-leaf2.cfg from files/site/nyc/rack1 (challenge): ...
│  ✓ a relative path to chi's README.txt from files/site/chi/rack2 (challenge): ...
│  ✓ the folder your home folder is in (challenge): ...
╰─ 4/4 checks passed  verified