Find your way around the terminal
The last page explained what a terminal is. This page teaches the commands you will use every day, one at a time. For each one you will see what it does, why you would type it, and then try it.
The black boxes below are practice terminals. They run in your browser and are not connected to your computer, so nothing you type can change or break your files. Click a box to start typing, and press Enter to run a line. Start over puts a box back to how it began.
A practice terminal is not the same as the Python exercises you will meet in later lessons:
| Practice terminal | Python exercise cell | |
|---|---|---|
| Looks like | Black box with a prompt (~/python-projects %) |
Dark editor with a Run button |
| You type | Terminal commands such as pwd and ls |
Python code such as print("hi") |
| Does | Pretends to be a terminal, with a few commands | Runs your Python for real |
| Python in it? | No. print("hi") gives an error |
Yes |
Practice terminals only exist in this lesson, to help you get used to the idea.
pwd: where am I?
A graphical file browser always shows where you are, in a title bar or a path at the top. A terminal does not. At any moment you are "in" one folder, called the current directory (a directory is another word for a folder), and most commands act on that folder unless you say otherwise.
pwd stands for "print working directory". It prints the full address of the
folder you are in:
~/python-projects % pwd
/home/student/python-projects
The text after the % is what you typed. The next line is the answer.
Picture your folders as a tree. You are standing at one spot on it, and pwd
reads out the address:
You type pwd whenever you are unsure where you are. Running a command in the
wrong folder is one of the most common beginner mistakes, and pwd is the
quickest way to check.
Run pwd in the practice terminal.
✓ Done. You reached the goal.
Hint 1
Type the three letters, then press Enter.
ls: what is here?
A terminal does not show you the contents of a folder until you ask. ls is
short for "list". It prints the names of the files and folders inside the folder
you are in. It is the same as opening that folder in Finder or File Explorer.
You type ls after you arrive somewhere, to see what you have to work with. A
new folder is empty, so the list is empty:
~/python-projects % ls
~/python-projects %
mkdir and cd: make a folder, step inside
mkdir is short for "make directory". It makes a new folder inside the current
one. You type it to keep your work organized. Later you will make a folder for
each project.
cd is short for "change directory". It moves you into a folder, like
double-clicking it. The folder you name is the argument:
~/python-projects % mkdir practice
~/python-projects % ls
practice
~/python-projects % cd practice
~/python-projects/practice % pwd
/home/student/python-projects/practice
Notice that the prompt changes to show the folder you are in. To go back up one
level, use cd ... Two dots mean "the folder above this one". One dot means
"this folder". A lone cd takes you to your home folder.
Make a folder called practice, then go into it.
✓ Done. You reached the goal.
Hint 1
Make the folder first. A folder has to exist before you can go into it.
Hint 2
Check with pwd or ls if you are not sure what happened. The prompt also shows the folder you are in.
Paths: how you name a place
The text after cd is a path, a way of naming a folder. A path can start
from where you are, or from the top of the computer.
| Path | Meaning |
|---|---|
notes |
the notes folder inside the current folder |
notes/week1 |
week1, inside notes, inside here |
.. |
the folder above this one |
/home/student/python-projects |
the same place from anywhere (starts with /) |
~ |
your home folder |
The top of the computer's folders is called the root, and it is written /.
Every other folder is inside it. Your home folder is where your own files
live, and it is written ~.
A relative path is the path from the place you are now. If you are in
python-projects, notes/week1 means the notes folder here, and then week1
inside it. An absolute path is the full path to a place, written from the
top. It means the same place no matter where you are. A quick way to tell them
apart: an absolute path starts with / or ~.
Recall
What do / and ~ stand for at the start of a path?
Show the answer
/ is the root, the very top folder, which holds every other folder on the computer. ~ is your home folder, where your own files live.
mkdir -p makes every folder along a path in one go. The -p is an
option: a word starting with a dash that changes how a command behaves. You
type -p when you want to make a folder inside a folder that does not exist
yet.
Make a folder notes with a folder week1 inside it, using one mkdir command. Then go into week1.
✓ Done. You reached the goal.
Hint 1
Without -p, mkdir refuses to make week1 while notes does not exist. Try it and read the error.
touch, echo and cat: files
Folders hold files, and the Python programs you write will be files. Three commands let you make and read them without opening an editor.
touchmakes an empty file. You type it to create a file you plan to fill in later, such astouch hello.py. On Windows,touchworks in Git Bash but not in PowerShell.echoprints the words you give it. It is how a command line says something out loud. The symbol>sends those words into a file instead of the screen.catprints what is inside a file. You type it for a quick look at a file without opening it.
~/python-projects % echo hello world > hello.txt
~/python-projects % cat hello.txt
hello world
Make a file called hello.txt that contains the words hello world. Then print it with cat.
✓ Done. You reached the goal.
Hint 1
echo hello world prints the words. Add > hello.txt after them to put them in a file instead.
ls and tree: the top level, or everything
ls shows only what is directly inside the folder you are in. It does not look
inside the folders it lists. tree draws every folder and file below you,
including the ones nested inside other folders. You type ls for a quick look
around and tree to see the whole shape of your work at once.
This practice terminal starts with some folders and files already in it, so there is something to see.
Run ls and notice that it lists only the top level. Then run tree and notice
everything nested inside the folders.
✓ Done. You reached the goal.
Hint 1
Run ls first, then tree. Compare how much each one shows.
clear and exit
clearwipes the screen when it gets cluttered. Nothing is deleted.exitends the session. It closes the terminal window or takes you back to the shell that started it.
Names are exact
The terminal does what you type, not what you meant.
- Capital letters matter.
Notesandnotesare different folders. - A space splits a line into separate words.
mkdir my notesmakes two folders,myandnotes. To make one folder with a space in its name, put quotes around it:mkdir "my notes". Most programmers avoid spaces in names for this reason and writemy-notesormy_notes.
Two tricks that save typing
- Press the Up arrow to bring back a command you already ran.
- Type the first few letters of a file or folder name and press Tab. The
terminal fills in the rest. In a practice terminal, make a folder called
practice, then trycd praand press Tab.
When something goes wrong
A mistyped command gives an error, and that is fine:
~/python-projects % lss
zsh: command not found: lss
It says the computer does not know a command called lss. Check the spelling
and try again. Reading the error is the first step in fixing anything, in the
terminal and later in your Python code.
Errors also tell you about places. cd: no such file or directory: notes means
there is no folder called notes here. Run ls to see what is here.
Commands to keep handy
| Command | What it does |
|---|---|
pwd |
Shows which folder you are in |
ls |
Lists the files in the current folder |
mkdir name |
Makes a new folder called name |
mkdir -p a/b |
Makes a and b inside it, in one go |
cd name |
Moves into the folder name |
cd .. |
Moves up one folder |
touch name |
Makes an empty file called name |
cat name |
Prints what is inside the file name |
tree |
Draws the folders and files as a tree |
clear |
Clears the screen |
exit |
Ends the terminal session |
Check your understanding
Work out the commands yourself. Open the hints only if you are stuck.
Set up this layout, then end up inside notes:
notes
└── week1
└── todo.txt
Use tree to check your work.
✓ Done. You reached the goal.
Hint 1
You can make the folders and the file without leaving the starting folder, by giving paths such as notes/week1/todo.txt.
Hint 2
You finish inside notes, one level above week1.
Here is a terminal with no goal, started with the same folders and files. Try anything from the table above.
Experiment. Make mistakes on purpose and read what comes back.
Try it on your real terminal
The practice terminal behaves like the real one, but the real one is the one you will use. Open the terminal on your own computer (how to find it) and try the same commands in a scratch folder.
- On a Mac or Linux computer, everything on this page works, apart from the
paths, which start with your own home folder instead of
/home/student. - On Windows, PowerShell understands
pwd,ls,cd,mkdir,echoandcat. Skip-p, because PowerShell'smkdiralready makes every folder along the path.touchdoes not exist there, andtreeprints a different picture. Paths use backslashes, likeC:\Users\yourname.
On Windows the next page installs Git Bash, a terminal that behaves the same way as the examples here. If you stay with PowerShell, keep the differences above in mind.
Next: Install your terminal.