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.
answers/1.2.ymlopen a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter
| Lesson | The shell and the filesystem |
|---|---|
| The tree | files/site/: two sites, two racks each, one leaf switch’s config per rack |
| Your answers | answers/1.2.yml |
new to this? step by stepbeginner
cd files/site, thenls,cdinto folders,cd ..to come back up, andpwdwhenever you’re lost.- Find chi-leaf2.cfg. Its absolute path is
pwdplus its name. cd files/site/nyc/rack1. Which path, with.., gets from there to the file? Try it withcat.- Fill in answers/1.2.yml, then
make check T=1.2.
more in meet the lab
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
pwdprints. It’s shorter, and it’s what you type most of the time.
Two names help you move around in relative paths:
| Name | Means |
|---|---|
. | 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.
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.
Up and down the tree
See the whole tree at once:
run in the terminalls -R files/siteWalk down into one rack. Type
cd files/si, press Tab, thennand Tab again, and watch the shell fill the names in:run in the terminalcd files/site/nyc/rack2 pwd lsRead a file one folder up, without leaving:
run in the terminalcat ../README.txtClimb back up, one folder at a time, and check where you are each time:
run in the terminalcd .. pwd cd ../../.. pwdYou’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.
Find chi-leaf2
Fill in answers/1.2.yml:
chi_leaf2_absolute: the absolute path ofchi-leaf2.cfg, wherever it is underfiles/site.from_nyc_rack1: a relative path to that same file, starting fromfiles/site/nyc/rack1. Check it:cdinto that folder andcatyour path. If it prints the config, it’s right.readme_from_chi_rack2: a relative path tofiles/site/chi/README.txt, starting fromfiles/site/chi/rack2.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.2when 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