Terminal start Lesson 3 of 12
Paths: absolute, relative, ~ and ..
9 minutes
Learning goals
- Picture the file system as a tree with / at the top.
- Tell absolute paths from relative ones and use both with cd.
- Move around with cd .., cd ~ and cd -.
- Write names with spaces using quotes or a backslash.
The tree
All files on Linux hang in one big tree. At the very top is the root, written as a single /. Under it are folders
like home, etc or tmp. Under home lives lerner, your home folder, and in there your own folders:
ls /
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr
The / between names separates the levels: /home/lerner/projects/website means root, then home, then lerner, then
projects, then website.
Absolute and relative
A path that starts with / is absolute. It always starts at the root and works no matter where you are:
cd /home/lerner/projects
Purpose: Jumps to a folder by its full path, from wherever you are.
Syntax: cd /PATH/FROM/THE/ROOT
Example:
cd /home/lerner/projects
pwd
/home/lerner/projectsA path without / at the start is relative: the shell starts looking in your working folder. In your home folder,
cd projects leads to the same place. Shorter, but only from here.
~ is a shortcut for your home folder. ~/projects is the same as /home/lerner/projects, from anywhere. And cd ~,
just like a plain cd, takes you home.
Up and back
.. is the folder above. You can chain it: ../.. is two levels up.
cd ..
Purpose: Goes one folder up.
Syntax: cd ..
Example:
cd projects/website
cd ..
pwd
/home/lerner/projectscd - takes you back to the folder you were in before, like the back button of a browser. It prints where it took you:
cd -
Purpose: Goes back to the folder you were in before and prints its path.
Syntax: cd -
Example:
cd /tmp
cd -
/home/lerner
cd -
/tmpNames with spaces
The shell splits your line at spaces. cat Documents/Holiday 2027.txt would look for two files, Documents/Holiday and
2027.txt. To keep the name together, put it in quotes:
cat "Documents/Holiday 2027.txt"
Purpose: Keeps a name with a space together as one name.
Syntax: "NAME WITH SPACE" or NAME\ WITH\ SPACE
Example:
cat "Documents/Holiday 2027.txt"
Where: the coast
When: July
Budget: 1200Or put a backslash right before the space. It tells the shell: this space is part of the name.
cat Documents/Holiday\ 2027.txt
Where: the coast
When: July
Budget: 1200
ls shows such names in single quotes, like 'Holiday 2027.txt', so you can see where the name starts and ends.
Practice
Terminal task 1/2
Jump into the folder website inside projects, then show with pwd where you are.
Task 1/2: Jump into the folder website inside projects, then show with pwd where you are.
Jump into the folder website inside projects, then show with pwd where you are.
Task complete
Solution
cd /home/lerner/projects/website
pwd Why: With a full path you jump in one step instead of walking folder by folder.
Another way
cd projects/website
pwd The relative path is shorter, but only works from your home folder. The absolute path works from anywhere.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Terminal task 2/2
Show the content of the file Holiday 2027.txt in the folder Documents.
Task 2/2: Show the content of the file Holiday 2027.txt in the folder Documents.
Show the content of the file Holiday 2027.txt in the folder Documents.
Task complete
Solution
cat "Documents/Holiday 2027.txt" Why: Without quotes the shell would split the name at the space and look for two files that do not exist.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Predict the output
Command
cd projects/website; cd ..; pwdBefore the command, in your home folder ~
- ~/.bashrc
- ~/.profile
- ~/.config/
- ~/Documents/
- ~/Downloads/
- ~/Pictures/
- ~/notes.txt
- ~/projects/
Check works once you have chosen an answer.
Fill the gap
You are in /home/lerner/projects/website. To get to /home/lerner with .. you type: cd
You are in /home/lerner/projects/website. To get to /home/lerner with .. you type: cd …
Check works once you have typed an answer.
Put in order
Put the levels of /home/lerner/projects/website in order, from the root down.
Steps
- projects
- website
- / (the root)
- home
- lerner (your home folder)
Now that you can get anywhere, the next lesson is about making things: new folders and files.
Quiz
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
-
Check works when exactly 2 are chosen. Chosen: 0.
-
Check works once you have chosen an answer.
Cards
-
Card 1 of 4
Tap the card or press Space to turn it over.
-
Card 2 of 4
Tap the card or press Space to turn it over.
-
Card 3 of 4
Tap the card or press Space to turn it over.
-
Card 4 of 4
Tap the card or press Space to turn it over.
End of the lesson
Still open: 5 exercises, 4 quiz questions
What you can do now: cd /home/lerner/projects, cd .., cd -, cat "Documents/Holiday 2027.txt"
+0 XP in this lesson
- Lesson 1: open
- Lesson 2: open
- Lesson 3: done
- Lesson 4: open
- Lesson 5: open
- Lesson 6: open
- Lesson 7: open
- Lesson 8: open
- Lesson 9: open
- Lesson 10: open
- Lesson 11: open
- Lesson 12: open
Path complete
You finished all 12 lessons of Terminal start.
On to Linux Basics: lesson 1, Finding your way around a strange server (8 min) Back to the path
Cheat sheet
-
cd /path/to/folderjump to a folder by its absolute path -
cd ~go to your home folder -
cd -back to the previous folder -
"name with space"keep a name with spaces together