Lesson 1 of 6

The terminal: pwd, ls, cd

About 15 minutesWalk your folders by typing instead of clicking.

The terminal is a window where you type instructions instead of clicking. It looks old-fashioned, but it's how almost every programming tool works, including the AI ones. You only need three commands to start: pwd, ls and cd.

Open a terminal

ComputerHow to open it
MacPress ⌘ Space, type Terminal, press Return.
WindowsPress the Start key, type Terminal (or PowerShell), press Enter.
LinuxUsually Ctrl Alt T.

You'll see a line ending in $ or % or > and a blinking cursor. That line is the prompt: the computer waiting for you. Type a command, press Enter, read what comes back. That's the whole loop.

The one big ideaA terminal is always standing in one folder, called the working directory. Every command you type happens there. Most “it doesn't work” moments are really “I was standing in the wrong folder”.

Practice on this one first

This desk computer runs a pretend terminal with the same folders as the filing cabinet in the Preliminaries. Click it and type, or tap the command buttons. Nothing you do here can break anything.

pwd: where am I?

Print working directory. It prints the absolute path of the folder you're standing in.

Terminal
$ pwd
/Users/you

When a terminal opens, you start in your home folder (~).

ls: what's here?

List. It shows the files and folders in the folder you're standing in.

Terminal
$ ls
Desktop    Documents    Downloads    Music    Pictures

Add -la for the long version: one line per item, with sizes in bytes, dates, and hidden files whose names start with a dot (like .gitignore).

Terminal
$ ls -la
drwxr-xr-x   5 you  staff   160 Oct 28 09:12 .
-rw-r--r--   1 you  staff    29 Oct 28 09:12 hello.js
drwxr-xr-x   3 you  staff    96 Oct 28 09:10 photos

A line that starts with d is a directory (folder). The number before the date is the size in bytes: hello.js is 29 bytes, exactly the count you saw in the Preliminaries.

cd: go somewhere

Change directory. You give it a path, absolute or relative, and the terminal moves there.

Terminal
$ cd Documents
$ pwd
/Users/you/Documents
$ cd ..
$ pwd
/Users/you
You typeYou go to
cd codeThe code folder inside where you are now
cd ..Up one level, to the folder that contains this one
cd ~ or just cdHome, from anywhere
cd /The very top of the disk
cd -Back to the folder you were in before
Two keys that save hoursTab finishes a file or folder name for you: type cd Doc and press Tab. ↑ brings back your last command so you can run it again or edit it.

Make things: mkdir and touch

Two more and you can set up a project without touching the mouse.

Terminal
$ mkdir my-site
$ cd my-site
$ touch index.html
$ ls
index.html

mkdir makes a directory. touch makes an empty file (zero bytes) if it doesn't exist yet. On Windows PowerShell, pwd, ls, cd and mkdir all work the same way; instead of touch, use ni index.html (short for New-Item).

Careful with rmYou'll see rm (remove) in tutorials and AI suggestions. It deletes files for good: there's no Trash. Read any rm command twice, and never run one you don't understand, especially with -rf in it.

Predict the output

Reading a terminal is mostly predicting what happens next. Try these without the figure, then check yourself on the desk computer above.

Your real terminal, for real

Do this once on your own computer. It makes the folder you'll use for the rest of the path.

Terminal
$ cd ~
$ mkdir hello
$ cd hello
$ pwd

Then open that folder in your file browser (on a Mac: open ., on Windows: explorer ., the dot means “this folder”) and see that the terminal and the windows you click on are looking at the same place.

Remember
  • The terminal always stands in one folder. pwd tells you which.
  • ls lists what's there. cd name goes in, cd .. goes up, cd ~ goes home.
  • mkdir makes a folder, touch makes an empty file.
  • Tab completes names. ↑ repeats. Read rm twice.