Terminal basics: files, folders, and commands · 第 3 / 6 节

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 cd will 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:

text
/└── Users    └── alex        ├── Desktop        └── Documents            └── notes.txt

Run these commands and watch how the path in the prompt changes.

text
$ pwd/Users/alex$ cd Documents$ pwd/Users/alex/Documents$ cd ..$ pwd/Users/alex$ cd ~/Documents$ pwd/Users/alex/Documents

Notice:

  • cd Documents used a relative path from /Users/alex.
  • cd .. moved up one level, back to /Users/alex.
  • cd ~/Documents used 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:

text
$ cd __$ cd Desktop

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

  1. William Shotts, Learning the Shell: Navigation — https://linuxcommand.sourceforge.io/lc3_lts0020.php 2 3 4

  2. Red Hat: 8 essential Linux file navigation commands for new users — https://www.redhat.com/en/blog/Linux-file-navigation-commands 2

练习

01

Run cd ~ and then pwd. Confirm the path printed is your home folder.

Level 1 (warm-up)
完成标准 · 本地勾选
02

Starting from your home folder, use one cd command with .. to move from a subfolder back to your home folder. For example, move into Documents, then return home with cd ...

Level 2 (advanced)
完成标准 · 本地勾选