The Terminal, Files and Paths ⌨️
The black window with the blinking cursor is not a hacker thing. It is a text conversation with your computer, and it is about to become the room you live in.
Why bother, when you have a mouse
Clicking is great for one file. It is terrible for four thousand. The terminal is how you say "rename every photo in this folder" in one sentence instead of four thousand drags. It is also the only sane way to run Python programs, install packages, and use almost every professional development tool ever built.
More importantly: it is a text interface, which means every instruction in every tutorial you will ever read is a line you can copy. Nobody can screenshot a mouse gesture.
It looks like the interface from a film where someone says 'I'm in'. You are going to type something wrong and destroy the machine.
You are not. You would have to go well out of your way. The commands in this lesson look at things and move you around; not one of them deletes anything.
Opening it
| System | How | What you get |
|---|---|---|
| macOS | Cmd+Space, type 'Terminal', Enter | zsh, a Unix shell |
| Windows | Start menu, type 'PowerShell', Enter | PowerShell |
| Linux | Ctrl+Alt+T, usually | bash or zsh |
You will see a prompt: some text, then a cursor. It is telling you where you are and waiting. You type a command, press Enter, it does the thing, and gives the prompt back.
Where am I? The idea of a path
Your files live in a tree. A path is the route from somewhere to a specific file, written as folder names joined by slashes:
/Users/you/Documents/python/hello.py macOS and Linux
C:\Users\you\Documents\python\hello.py Windows (backslashes)
Two kinds of path, and the difference causes more beginner pain than any other topic:
- An absolute path starts at the very top (
/orC:\) and works everywhere. Like full postal address. - A relative path starts from wherever you currently are.
hello.pymeans "in this folder". Like saying "two doors down".
Two special names you will use constantly:
| Written | Means |
|---|---|
. | the folder I am in right now |
.. | the folder one level up |
~ | my home folder (macOS/Linux, and PowerShell understands it too) |
python hello.py failing with can't open file 'hello.py': No such file or directory almost never means the file is missing. It means you are not standing in the folder that contains it. Run ls (or dir on Windows) and look. If the file is not in that list, you need to cd somewhere else first.
The ten commands that cover almost everything
| macOS / Linux | Windows PowerShell | What it does |
|---|---|---|
pwd | pwd | Print working directory: where am I? |
ls | ls or dir | List what is in this folder |
ls -la | ls -Force | List everything, including hidden files |
cd folder | cd folder | Go into a folder |
cd .. | cd .. | Go up one level |
cd ~ | cd ~ | Go home |
mkdir name | mkdir name | Make a folder |
cat file | cat file | Print a file to the screen |
python3 file.py | python file.py | Run a Python program |
clear | cls | Wipe the screen (nothing is deleted) |
A first session, annotated
~ $ pwd
/Users/you
~ $ mkdir python-school
~ $ cd python-school
~/python-school $ pwd
/Users/you/python-school
~/python-school $ ls
(nothing here yet, which is correct)
~/python-school $ cd ..
~ $
That is the whole loop: look around, move, make something, look again. You now know enough terminal to finish this entire course.
Three tricks that will save you hours
- Tab completion. Type the first few letters of a folder or file and press Tab. The shell finishes it. This prevents typos and is the single biggest speedup available to you. Use it constantly.
- Up arrow. Recalls your previous commands. You will re-run the same
python hello.pythree hundred times today. Do not retype it. - Ctrl+C. Stops whatever is running. If a program is stuck in a loop and will not stop, this is the escape hatch. Memorise it now, you will need it in Lesson 8.
Watch an experienced developer's hands sometime. They barely type full paths. It is all Tab, up arrow, Ctrl+R, muscle memory built over years. None of it is talent. It is just the same six keys, ten thousand times.
Dragging beats typing
Nobody types long paths. On macOS and Linux, type cd (with the space) and
then drag the folder from your file manager onto the terminal window.
The path appears. Press Enter. On Windows, hold Shift, right-click a folder, and choose
"Copy as path", then paste.
Find your feet
Open a terminal and run these, in order. Read each result before typing the next one.
pwd
ls
cd ~
mkdir python-school
cd python-school
pwdReveal solution
You should end with a path ending in /python-school (or \python-school on Windows). That folder is where everything in this course will live.
If mkdir said the folder already exists, no harm done: you have run it twice, which is exactly as damaging as it sounds.
Read a path
You are in /Users/you/python-school. Where do these point?
notes.txt../Downloads/data.csv/etc/hosts
Reveal solution
/Users/you/python-school/notes.txt, in the folder you are standing in./Users/you/Downloads/data.csv: up one level, then down into Downloads./etc/hostsexactly, because it starts with a slash. Absolute paths ignore where you are standing.