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

Lesson 2: Find where you are and move around

Lesson objectives:

  • Know what your current working directory is.
  • Use pwd to print your current location, ls to list contents, and cd to change folders.
  • Predict what folder you will be in after each command.

Prerequisites: Lesson 01 (opening the terminal, reading the prompt, running a command) | Previous << 01 | Next 03 >>

Lost in the maze of folders

Your computer stores files in a tree of folders. When you open a terminal, you are standing in one specific folder. That folder is called your current working directory 1. If you do not know where you are, commands that read or write files will act on the wrong place.

Three commands solve this:

  • pwd (print working directory) tells you where you are 21.
  • ls (list) shows the files and folders in your current directory 23.
  • cd (change directory) moves you to a different folder 21.

Think of pwd as checking your street address, ls as looking around the room, and cd as walking through a door.

Worked example (follow along)

Open your terminal and run these three commands one at a time. Press Return after each.

text
$ pwd/Users/alex$ lsApplications  Desktop  Documents  Downloads  Pictures$ cd Desktop$ pwd/Users/alex/Desktop$ lsresume.pdf  todo.txt

What happened:

  1. pwd showed you were in /Users/alex.
  2. ls showed the folders inside it.
  3. cd Desktop moved you into the Desktop folder.
  4. pwd now prints /Users/alex/Desktop.
  5. ls shows the files on the Desktop.

Your turn (faded example)

You are in /Users/alex. You want to move into the Documents folder and list its contents. Fill in the blanks:

text
$ ____ Documents$ ____

Answer: cd Documents, then ls.

Summary + what's next

You now know how to check where you are (pwd), look around (ls), and walk through a door (cd). In Lesson 3, you will learn the two ways to describe a folder's location: absolute paths and relative paths.

Footnotes

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

  2. UC Berkeley SCF: Using the bash (and zsh) shell — https://computing.stat.berkeley.edu/tutorial-using-bash/ 2 3

  3. MDN: Command line crash course — https://developer.mozilla.org/en-US/docs/Learn_web_development/Getting_started/Environment_setup/Command_line

练习

01

Open your terminal, run pwd, then run ls. Write down the folder name and one file or folder you see.

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

Navigate from your home folder to Desktop, then to Documents, and back to your home folder using only cd and pwd to check yourself.

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