Sub topic 3
Terminal basics you need
This chapter covers the few terminal skills the rest of the tutorial relies on. It assumes you can install software and find your files in a file manager. It does not assume you have used a terminal before.
What a terminal and a shell are
A terminal is a window where you type text commands instead of clicking. Inside it runs a shell, a program that reads each line you type, runs it, and prints the result. Which shell you have depends on your system:
- Windows: PowerShell (recommended) or Command Prompt. Open either from the Start menu.
- macOS: zsh, in the Terminal app.
- Linux: usually bash, in the Terminal app from your desktop menu.
The line where you type is the prompt. It often ends in $, %, or > and usually shows your current folder. Commands run only after you press Enter.
The working directory
The shell is always "in" one folder, called the working directory. Commands that use relative file names, such as python3 hello.py later in this tutorial, look for those files there. A common beginner error is saving a file in one folder while the terminal sits in another.
Commands you will use
| Task | macOS and Linux | Windows PowerShell |
|---|---|---|
| Show the working directory | `pwd` | `pwd` |
| List files | `ls` | `ls` or `dir` |
| Enter a folder | `cd projects` | `cd projects` |
| Go up one level | `cd ..` | `cd ..` |
| Create a folder | `mkdir hello-python` | `mkdir hello-python` |
| Clear the screen | `clear` | `cls` |
Here is a short session on macOS or Linux that creates a project folder and moves into it:
cd ~
mkdir hello-python
cd hello-python
pwd~ stands for your home folder. The last command prints the full path, for example /home/ada/hello-python. On Windows, the same commands work in PowerShell, and cd ~ also goes to your home folder.
Anatomy of a command
A command line has a program name followed by arguments, separated by spaces. Options that change behavior are often called flags and start with - or --. You will see this shape throughout the tutorial:
python3 -m venv .venvHere python3 is the program, -m is a flag, and venv and .venv are arguments. The shell splits on spaces, so a path that contains spaces must be quoted:
cd "C:\Users\Ada Lovelace\projects"Without the quotes, the shell would treat C:\Users\Ada and Lovelace\projects as two arguments.
Habits that save time
- Press Tab to complete file and folder names. Type
cd hel, press Tab, and the shell fills inhello-python. - Press the Up arrow to bring back the previous command and edit it.
- Press Ctrl+C to stop a command that is still running or seems stuck.
- Read the message after a failed command. It usually names the problem.
Common errors
- "No such file or directory" (bash or zsh) or "Cannot find path" (PowerShell): the name is misspelled, or you are in a different working directory. Run
pwdandlsto check. - "command not found" or "is not recognized": the shell cannot find a program by that name. This matters later when you install Python, so keep it in mind.
- Nothing seems to happen: the command may be waiting for input or still running. Try Ctrl+C.
Exercise
You saved hello.py in ~/projects/hello-python, but the terminal prompt shows you are in your home folder. Give the commands, in order, to confirm the file is there. Assume macOS or Linux.
Show answer
Run cd projects/hello-python to enter the folder. Run pwd to confirm the location. Run ls and check that hello.py appears in the listing.