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.

Three boxes side by side. A real terminal on your computer runs real commands. A practice terminal on a lesson page pretends safely. A Python exercise on a lesson page has a Run button and runs your Python.

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:

A folder tree with student at the top, then python-projects, then notes with week1 inside it, and practice beside notes. On the left, a marker shows you are in week1 and pwd prints the path /home/student/python-projects/notes/week1. On the right, after cd .. the marker has moved up to notes.

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.

Practice terminal: type commands here, not Python


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.

Practice terminal: type commands here, not Python


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.

Recall: click each blank to check yourself

An path is the path to a place, written from the top. It starts with (the root) or (your home folder), so it means the same place wherever you are.

Recall: click each blank to check yourself

A path is the path from the . From python-projects, notes/week1 means the notes folder inside it, and then week1.

Two copies of the folder tree. On the left, from python-projects the relative path notes/week1 leads to week1. On the right, the absolute path /home/student/python-projects/notes/week1 starts at the top and works from any folder.

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.

Practice terminal: type commands here, not Python


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.

~/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.

Practice terminal: type commands here, not Python


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.

Practice terminal: type commands here, not Python


Hint 1

Run ls first, then tree. Compare how much each one shows.

clear and exit

Names are exact

The terminal does what you type, not what you meant.

Two tricks that save typing

Recall: click each blank to check yourself

Press the to bring back a command you already ran. Type the first few letters of a name and press to fill in the rest.

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

Recall: click each blank to check yourself

shows which folder you are in. lists what is in it. makes a folder. moves into a folder. makes an empty file. prints what is inside a file.

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.

Practice terminal: type commands here, not Python


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.

Practice terminal: type commands here, not Python


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 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.