Finding Your Way in the Terminal

The terminal is where you run Python programs, and four commands that work the same in PowerShell and zsh, pwd, ls, cd and mkdir, tell you which folder you're in, show what's there, move you to another folder and make a new one.

Novice · Python · Operating System · Updated

By

After this lesson you can

  • Find out where you are in the terminal with pwd, list what's there with ls, and tell the terminal, the shell and the prompt apart
  • Move between folders with cd, cd .. and cd ~, and make a folder with mkdir

Key takeaways

  • The terminal is the window you type commands in, the shell is the program that reads them (PowerShell on Windows, zsh on a Mac), and the prompt shows it's waiting.
  • A command runs in one folder at a time, so a program you run has to be in the folder the terminal is in, or Python can't find it.
  • pwd prints the folder you're in and ls lists what's in it.
  • cd and a folder's name moves you into it, cd .. moves up one folder and cd ~ goes home. mkdir makes a new folder.
  • A command's first word names the program to run. Ctrl+C stops a program that's still running.

You write your first program, save it as hello.py, type python hello.py in the terminal, and Python answers that there's no such file. The file is right there in VS Code. What went wrong is where the terminal was: it was looking in a different folder. On a Mac, it looks like this:

python3: can't open file '/Users/sam/hello.py': [Errno 2] No such file or directory

On Windows the message starts with Python's full path instead of python3, and ends the same way. The terminal was in sam's home folder, and the file was saved somewhere else. Four short commands fix this for good: they tell you where you are, what's there, how to move, and how to make a new folder. You'll use them every time you run a program, install a package or get exercise code.

The terminal, the shell and the prompt

Think of texting a very literal assistant. You type one instruction, send it, and read the reply; the assistant does exactly what you typed, never what you meant. Three words name the parts:

  • The terminal is the window you type in. VS Code has one built in: choose Terminal > New Terminal.
  • The shell is the program inside it that reads your command and runs it. On Windows it's PowerShell; on a Mac it's zsh. The commands in this lesson are typed the same in both.
  • The prompt is the text before the cursor that says the shell is waiting. It usually shows the folder you're in:
PS C:\Users\Sam\python-course>

That's PowerShell's prompt. On a Mac, zsh's ends with % instead, such as sam@Sams-MacBook python-course %. Type a command after the prompt, press Enter, and the answer appears under it, followed by a new prompt.

Where am I? pwd and ls

The terminal is always "in" one folder, called the current folder, and every command you type works there. pwd (print working directory; a directory is another word for a folder) prints its full address, its path:

pwd

PowerShell answers with a short table:

Path
----
C:\Users\Sam\python-course

On a Mac, zsh prints one line: /Users/sam/python-course. ls (list) shows what's in the current folder:

ls
maple-street-bakery  notes.txt

That's zsh's answer: here, one folder and one file. PowerShell shows the same names in a table, with columns for each item's kind, when it last changed and its size; the names are in the Name column.

Moving around: cd

cd (change directory) and a folder's name moves you into that folder. Then pwd shows where you landed:

cd maple-street-bakery
pwd
/Users/sam/python-course/maple-street-bakery

That's a Mac's answer; on Windows it's C:\Users\Sam\python-course\maple-street-bakery, in the same short table as before. The prompt changes too, to show the new folder. Two short names get you anywhere:

  • cd .. (read "cd dot dot") moves up one folder, to the one that holds this one.
  • cd ~ (read "cd tilde") goes to your home folder from wherever you are.

Typing long folder names is slow. Type the first few letters, such as cd map, and press Tab: the shell finishes the name for you.

Making a folder, and running a program

mkdir (make directory) and a name makes a new, empty folder inside the current one:

mkdir menu-board

zsh prints nothing, which means it worked; PowerShell prints a few lines about the new folder. Check with ls, then cd menu-board to go in.

Every command works the same way: its first word names the program to run, and the words after it tell that program what you want. git --version runs Git and asks its version; python hello.py runs Python and gives it a file. That's why the current folder matters: Python looks for hello.py in the folder the terminal is in.

Most programs finish and hand you back the prompt. When one keeps running and you want it to stop, press Ctrl+C (Ctrl and C together, on a Mac too). You'll need it the day a program of yours doesn't stop.

Try it in the terminal

Terminal Quest: The Lighthouse, goals 1 to 4: played right on the lesson page.

Your turn, in a practice terminal right on this page: find out where you are, look around, read the welcome note, and go into the cottage. Pick PowerShell or zsh's cousin, bash; the four commands are the same. To read the note, type cat and the note's name; the quest's hint shows you how.

Check yourself

2 questions to try on the lesson page.

So what

pwd, ls, cd and mkdir are how you get to the right folder before you run anything, and being in the right folder is the fix for one of the most common errors a beginner meets.

Key terms