A server with no mouse
On your first week, a colleague asks you to find last month's invoices on a server.
There is no desktop and no mouse. There is only a window with a blinking cursor.
Most servers work like this. You move round them by typing short commands.
By the end of this lesson
Note
I can move round files and folders from a terminal.
Key words
| Word | What it means | Example |
|---|---|---|
| Terminal | a window where you type commands | |
| Command | an instruction you type and run | ls |
| Folder | a place that holds files; also called a directory | |
| Path | the address of a file or folder | /home/sam/shop |
| Working directory | the folder you are in now | |
| Shell | the program that reads and runs your commands |
The cells students fill in are left blank in this preview.
pwd and ls tell you where you are
The shell always has a working directory. Commands act on that folder unless you say otherwise.
pwd prints the path of the folder you are in. ls lists what is in it.
cd changes folder. cd invoices goes into the folder called invoices.
Look round a shop folder
# Make a small shop folder to practise on.
mkdir -p shop/invoices shop/orders
printf 'Invoice 101, 45.00\n' > shop/invoices/inv-101.txt
printf 'Invoice 102, 12.50\n' > shop/invoices/inv-102.txt
printf 'Order 2001, 3 mugs\n' > shop/orders/ord-2001.txt
# Where am I, and what is here?
cd shop
pwd
ls
# Look inside one folder without going into it.
ls invoices
A path is the address of a file
A path that starts with / is a full path. It works from any folder.
A path without / at the start is a relative path. The shell looks for it inside your working directory.
Two dots, .., mean the folder one level up. So cd .. takes you back out.
Worked example: where cd takes you
| You are in | You type | You end up in |
|---|---|---|
| /home/sam | cd shop | /home/sam/shop |
| /home/sam/shop | cd invoices | /home/sam/shop/invoices |
| /home/sam/shop/invoices | cd .. | /home/sam/shop |
| /home/sam/shop | cd /var/log | /var/log |
| /var/log | cd invoices | An error: no such folder |
The cells students fill in are left blank in this preview.
A common mistake
Warning
Many people think cd invoices works from any folder. It only looks inside the folder you are in now. Run pwd first to see where you are, or use a full path that starts with /.
Go down and back up
mkdir -p shop/invoices shop/orders
cd shop/invoices
pwd
# .. means the folder one level up.
cd ..
pwd
# Go into orders, then back up again.
cd orders
pwd
cd ..
ls
mkdir, cp and mv change things
mkdir archive makes a new folder called archive.
cp old new copies a file. The original stays where it was.
mv old new moves a file. If you give a new name in the same folder, it renames the file instead.
Tidy up the shop folder
mkdir -p shop/invoices shop/orders
printf 'Ring the courier at 3pm\n' > shop/notes.txt
cd shop
# Copy a file: the original stays where it is.
cp notes.txt notes-backup.txt
# Make a new folder for this month.
mkdir archive
# Move the backup into it.
mv notes-backup.txt archive/
ls
ls archive
# Your turn: move notes.txt into the orders folder,
# then run ls orders to check it arrived.
What a hiring manager expects
Tip
Most technical jobs expect you to find a file on a server without help. If you can use pwd, ls and cd calmly, you can follow almost any guide your team writes.