Lesson 3: Use paths to reach any folder
Lesson objectives:
- Tell the difference between an absolute path and a relative path.
- Use
~to jump to your home folder and..to move up one folder.- Predict where
cdwill land based on the path you give it.Prerequisites: Lesson 02 (
pwd,ls,cd) | Previous << 02 | Next 04 >>
Why does cd Documents work but cd /Documents fail?
A path is a set of directions to a folder. There are two ways to give those directions.
An absolute path starts from the root of the file system, the very top of the tree. It always begins with / 1. For example, /Users/alex/Documents is an absolute path. No matter where you are, it means the same folder.
A relative path starts from where you are right now. It does not begin with / 1. For example, Documents means "the Documents folder inside my current working directory." If you are in /Users/alex, Documents points to /Users/alex/Documents. If you are somewhere else, it points to a different place or nowhere at all.
Two shortcuts make this faster:
~is shorthand for your home folder 12...is shorthand for the parent folder, the one directly above your current folder 12.
Worked example (follow along)
You are in /Users/alex, and you know the folder tree looks like this:
Run these commands and watch how the path in the prompt changes.
Notice:
cd Documentsused a relative path from/Users/alex.cd ..moved up one level, back to/Users/alex.cd ~/Documentsused an absolute path from the home shortcut.
The diagram shows the folder tree: Documents lives inside /Users/alex, and notes.txt lives inside Documents. From Documents, .. climbs back to /Users/alex.
Your turn (faded example)
You are in /Users/alex/Documents. You want to go back to /Users/alex and then into Desktop. Fill in the blank:
Answer: cd .. (or cd /Users/alex).
Summary + what's next
You now know that absolute paths start from / and relative paths start from where you are. You also know the shortcuts ~ for home and .. for parent. In Lesson 4, you will open files and look at their contents without leaving the terminal.
Footnotes
-
William Shotts, Learning the Shell: Navigation — https://linuxcommand.sourceforge.io/lc3_lts0020.php ↩ ↩2 ↩3 ↩4
-
Red Hat: 8 essential Linux file navigation commands for new users — https://www.redhat.com/en/blog/Linux-file-navigation-commands ↩ ↩2