Skip to content

0.3 The command line

IGCSE and A LevelPart 0

Suggested time: 1 lesson (with Chapter 0.4) · You need: a computer with Python installed (Chapter 0.2)

What you will do

Most of the time you will run programs by pressing a Run button. Sooner or later, though, you will need the command line: to install libraries, to use Git, to run a program when your editor is misbehaving, or to read an error message in full. This chapter teaches the handful of commands you actually need.

By the end of this chapter you will be able to:

  • open a terminal and explain what the prompt is telling you;
  • find out which folder you are in, and list the files inside it;
  • explain the difference between an absolute path and a relative path;
  • move to any folder with cd, including folders with spaces in their names.

Key vocabulary

Term Meaning Example
Command line Controlling the computer by typing text commands instead of clicking cd Desktop
Terminal The window you type commands into Terminal (Mac), Command Prompt (Windows)
Shell The program inside the terminal that reads and carries out your commands zsh (Mac), cmd or PowerShell (Windows)
Prompt The text the shell shows when it is ready for a command. It usually shows the current folder C:\Users\alex>
Directory Another word for a folder Desktop
Current working directory The folder the terminal is "standing in". Commands act on it unless you say otherwise C:\Users\alex\Desktop
Path The address of a file or folder Desktop\CastleAdventure
Absolute path A full address from the top of the drive. It works from anywhere /Users/alex/Desktop
Relative path An address that starts from the current working directory code/stage_02
Parent directory The folder one level up, written as two dots ..
Argument Extra information typed after a command, separated by a space hello.py in py hello.py

The big idea: talking to the computer in text

When you double-click a folder you are using a graphical user interface (GUI). The command line is a command-line interface (CLI): you type an instruction, press Enter, and the shell carries it out and prints the result. Both are ways of asking the operating system to do the same jobs.

Concept: the command-line interface

How it works: a shell runs in a loop. It shows a prompt, waits for you to type a line, splits the line into a command and its arguments, runs the command, prints any output, then shows the prompt again. In py hello.py the command is py (start Python) and the argument is hello.py (the file to run).
Why we use it: it is precise and repeatable, since a command does exactly the same thing every time and you can copy it into instructions for someone else. Error messages appear in full. Some tools, such as pip, only have a command-line version.
Where else it's used: professional developers use the command line every day, for version control with Git, running tests, installing libraries, and controlling servers that have no screen at all.

Step 1: Open a terminal

Where How to open a terminal
Windows Press the Windows key, type cmd and press Enter. This opens Command Prompt. You may also have PowerShell or Terminal, which work almost the same way (see the box after Step 5).
macOS Press Cmd+Space, type Terminal and press Enter.
VS Code Choose Terminal → New Terminal. It opens already inside the folder you have open.
Thonny Choose Tools → Open system shell…. It is already set up to use Thonny's own Python.

Open a terminal already inside a folder

Windows: open the folder in File Explorer, click in the address bar, type cmd and press Enter.
macOS: in Finder, right-click the folder and look for New Terminal at Folder (it may be under Services).

Step 2: Read the prompt

The prompt tells you which folder you are in. You don't type it.

Command Prompt
C:\Users\alex>

C:\Users\alex is the current folder, and > means "ready".

Terminal
alex@MacBook-Air ~ %

alex is the user name, MacBook-Air the computer's name, ~ the current folder (your home folder), and % means "ready". Some systems show $ instead.

Step 3: Where am I? What's here?

Your files live in a tree of folders:

C:\
└── Users
    └── alex                  <- your home folder
        ├── Documents
        └── Desktop
            └── CastleAdventure
                └── step_05
                    └── main.py
Task Windows (Command Prompt) macOS / Linux
Show the current folder cd pwd
List what's in it dir ls
List only the Python files dir *.py ls *.py

Your turn

Show your current folder, then list what's in it. You should see folders such as Desktop, Documents and Downloads.

What's happening?

pwd stands for print working directory. On Windows, cd on its own does the same job. ls is short for list and dir for directory.

Step 4: Understand paths

Windows macOS / Linux
Absolute path: from the top of the drive; works from any folder C:\Users\alex\Desktop\CastleAdventure /Users/alex/Desktop/CastleAdventure
Relative path: from the folder you are in now CastleAdventure\step_05 CastleAdventure/step_05
Parent folder (one level up) .. ..
This folder . .
Home folder %USERPROFILE% ~

Windows separates folder names with a backslash \; macOS and Linux use a forward slash /. It's a good habit to type names with the right capital letters, because many systems (including Linux web servers) treat Main.py and main.py as different files.

Concept: absolute and relative paths

How it works: an absolute path is like a full postal address, which works wherever you are. A relative path is like directions ("through the door, second on the left"), which only make sense from a known starting point. The shell always combines a relative path with the current working directory.
Why we use it: relative paths are shorter and keep working when a whole project folder is moved or copied to another computer. That is why games load pictures with paths like assets/images/player.png.
Where else it's used: web pages link to images with relative and absolute URLs, and file handling in any language uses exactly the same idea.

Step 5: Move around with cd

cd means change directory. Type cd, a space, then the path. The prompt changes to show where you are.

cd Desktop
cd CastleAdventure\step_05
dir *.py
cd Desktop
cd CastleAdventure/step_05
ls *.py

Or jump straight there with an absolute path, from any folder:

cd C:\Users\alex\Desktop\CastleAdventure\step_05
cd /Users/alex/Desktop/CastleAdventure/step_05
Task Windows (Command Prompt) macOS / Linux
Up one folder cd .. cd ..
Up two folders cd ..\.. cd ../..
Across to a sibling folder cd ..\step_06 cd ../step_06
Home folder cd %USERPROFILE% cd (on its own) or cd ~
Another drive, e.g. a USB stick cd /d D:\Games USB sticks appear under /Volumes (Mac) or /media (Linux)

Folder names with spaces need quotation marks

The shell uses spaces to separate a command from its arguments, so a folder called Castle Adventure looks like two separate words. Put the path in double quotation marks: cd "Castle Adventure".

Three tricks that save a lot of typing

  1. Tab completion: type the first few letters of a name and press Tab. The shell finishes it for you, and adds quotation marks if needed.
  2. Drag and drop: type cd and a space, then drag a folder from File Explorer or Finder onto the terminal.
  3. Up arrow: press ↑ to bring back an earlier command.

Windows: is your Desktop really in OneDrive?

On many Windows computers, especially school ones, the Desktop and Documents folders are stored inside OneDrive. If cd Desktop says The system cannot find the path specified, try cd OneDrive then dir. The folder name varies between computers, so use Tab completion or drag-and-drop rather than guessing.

Using PowerShell instead of Command Prompt?

PowerShell's prompt starts with PS, for example PS C:\Users\alex>. Almost everything in this chapter works the same, with a few differences: pwd and ls also work; cd ~ goes home; to change drive use cd D:\Games (the /d from Command Prompt doesn't work).

Checkpoint

You can open a terminal, say which folder you're in, list its contents, and cd into a folder and back out again.

Troubleshooting

What you see What it means and what to try
The system cannot find the path specified / no such file or directory The folder isn't where you think. Use dir or ls to see what's really there. Check spelling, use Tab completion, and check OneDrive (Windows).
cd only goes to the first word of a folder name The name has a space in it. Put the path in quotation marks.
Nothing happens and there's no new prompt A program is still running. Press Ctrl+C.

There's no 'delete' command in this chapter

Files deleted from the command line do not go to the Recycle Bin. Delete files in File Explorer or Finder instead.

Check your understanding

  1. What is the difference between the terminal and the shell?
  2. You are in C:\Users\sam\Desktop. What is the absolute path of the folder you reach by typing cd Games\CastleAdventure?
  3. Give one advantage of a relative path over an absolute path when a game loads its pictures.
  4. Why does cd Castle Adventure not work, and how do you fix it?
Answers
  1. The terminal is the window that shows text and takes your typing. The shell is the program running inside it that interprets and carries out your commands (for example cmd, PowerShell or zsh).
  2. C:\Users\sam\Desktop\Games\CastleAdventure
  3. For example: the game still works when its folder is copied to another computer or user account, because the path doesn't include a user name or drive letter.
  4. The shell treats the space as the end of the argument, so it looks for a folder called Castle. Use cd "Castle Adventure" (or Tab completion).

Summary

  • The terminal is the window; the shell reads your commands. The prompt shows the current folder.
  • cd (Windows) or pwd (Mac/Linux) shows where you are; dir or ls lists what's there; cd moves.
  • Absolute paths start from the top of the drive; relative paths start from where you are; .. means up one.
  • Put paths with spaces in quotation marks. Use Tab, drag-and-drop and the Up arrow to save typing.
This is a free sample: log in for every book →