Linux on call · Chapter 2 of 30
Moving around
By the end of this chapter you can walk anywhere on the server with cd, write a file into the folder you mean, and build a set of folders in one command. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Finding your way
- 5 tasks
- Linux
- Free chapter
Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.
Why you need this
The new cook wants Arjun's masala chai recipe, and Ravi needs its secret ingredient written down in the chapter folder. The recipe is in Arjun's archive, several folders deep, and there is an old 2024 recipe in there too that nobody should follow. Kabir also wants a tidy home for the app before the next release: a tapri folder with places for the app, its logs and two kinds of backups, and a drawing of it for the team.
So far you have stayed in one folder and named everything from there. You could read the recipe with one very long path, but every typo means typing it again, and a file you save while standing deep in the archive lands deep in the archive. You need to move, know where you are, and get back.
The lesson
cd: change directory
cd takes a path, relative or absolute, and moves you there. pwd tells you where you landed:
cd /usr/share/doc
pwd
/usr/share/doc
Typing long paths is slow, so press Tab: the shell finishes a folder name as soon as only one name fits (type cd /usr/sh and press Tab to get cd /usr/share/), and pressing Tab twice lists the choices. A typo, or a file where a folder should be, gets an error and leaves you where you were:
bash: cd: /usr/shar: No such file or directory
bash: cd: archive/README.txt: Not a directory
Short paths
..is the folder above. From/usr/share/doc,cd ..lands in/usr/share, andcd ../..in/usr.~is your home folder,/home/student. A plaincdgoes there too.cd -jumps back to the folder you were in before, and prints it:
cd /etc
cd /usr/share
cd -
/etc
Press Next to follow one walk around the server:
Making folders
mkdir makes a folder, but only inside a folder that is already there:
mkdir kitchen/shelves/top
mkdir: cannot create directory 'kitchen/shelves/top': No such file or directory
With -p (parents) it makes every missing folder on the way, and does not complain about folders that already exist. Several paths can go in one mkdir:
mkdir -p kitchen/shelves/top kitchen/shelves/bottom
Where a file lands
A relative path in a command starts from the folder you stand in, and that includes the file name after >. From deep inside the new folders:
cd kitchen/shelves/top
echo "two jars of elaichi" > ../../note.txt
cd ../..
pwd
ls
/workspace/chapters/02-moving-around/kitchen
note.txt shelves
../../note.txt went two folders up, into kitchen. A plain > note.txt would have stayed in top. When you are lost, pwd tells you the truth.
Drawing a tree
tree draws a folder and everything under it, and counts what it found:
tree kitchen
kitchen
|-- note.txt
`-- shelves
|-- bottom
`-- top
4 directories, 1 file
The first line is the name you gave it. Run tree with no name from inside kitchen, and that first line is just . instead. > saves the drawing in a file, exactly as it saved echo's line in chapter 1.
Your turn
Your five tasks are at the top, and the terminal prints them too (lab tasks shows them again). Read archive/README.txt first: Arjun says which recipe is current. Watch where answer.txt lands, and run tree from the chapter folder. If the folder gets in a state you do not understand, lab reset builds the whole chapter folder again, so you would redo your other files too.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Walk into the current recipes folder
Walk into the folder with Arjun's current masala chai recipe.
1. Run cat archive/README.txt. It says which recipes folder is the current one.
2. Use cd to move into that folder. Do not use the old 2024 folder.
3. Run pwd. It should end in that recipes folder.
To pass: you used cd from the chapter folder, and you stood in the current recipes folder. A command with a typing mistake does not count, so type it again.
- Task 2
Save the secret ingredient in answer.txt
Save the secret ingredient from the current recipe in a file.
1. In the current recipes folder, open the masala chai recipe with cat.
2. Find the line that names the secret ingredient.
3. Save only the ingredient in answer.txt, in the chapter folder. The chapter folder is five folders above the recipes folder.
4. Run cat answer.txt from the chapter folder to see what you saved.
To pass: answer.txt is in the chapter folder (not in the recipes folder). It has the secret ingredient from the current recipe, and nothing else.
- Task 3
Go home and come straight back
Go to your home folder, then come back with one short command.
1. Run cd ~ (or just cd). It takes you to your home folder.
2. Run cd -. It takes you back to the folder you were in before.
3. Look at what cd - prints. It is the folder you came back to.
To pass: you went to your home folder, and then you ran cd -. A command with a typing mistake does not count, so type it again.
- Task 4
Make the tapri folders
Make a set of folders for the app, in one command.
1. Go to the chapter folder.
2. Make the folder tapri, with three folders inside it: app, logs and backups.
3. Inside tapri/backups, make two folders: daily and weekly.
4. Use mkdir -p. The -p makes the missing folders above a folder too.
To pass: the chapter folder has these folders: tapri/app, tapri/logs, tapri/backups/daily and tapri/backups/weekly.
- Task 5
Save the tapri drawing in layout.txt
Save a drawing of the tapri folder in a file.
1. Go to the chapter folder.
2. Run tree tapri. It draws tapri and the five folders inside it.
3. Save that same drawing in layout.txt, in the chapter folder. > sends the output into a file.
To pass: layout.txt has the drawing of tree tapri, run from the chapter folder. Its first line is tapri (not a dot), it shows all five folders, and it ends with the line that counts the directories.
What you will learn
- cd: change the folder you stand in, with short paths like .., ~ and -.
- Relative paths: where a file lands when you save it from another folder.
- mkdir -p: make a folder, and every missing folder above it.
- tree: draw a folder and everything under it.
More in Finding your way
Build "Moving around" yourself
Tapri’s ordering app moves onto a Linux server, and the server is now yours to run. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.