Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Files and folders in the terminal

After this lesson: I can move round files and folders from a terminal.

45 min lessonNo commands

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

WordWhat it meansExample
Terminala window where you type commands
Commandan instruction you type and runls
Foldera place that holds files; also called a directory
Paththe address of a file or folder/home/sam/shop
Working directorythe folder you are in now
Shellthe 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

Code: bash
# 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 inYou typeYou end up in
/home/samcd shop/home/sam/shop
/home/sam/shopcd invoices/home/sam/shop/invoices
/home/sam/shop/invoicescd ../home/sam/shop
/home/sam/shopcd /var/log/var/log
/var/logcd invoicesAn 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

Code: bash
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

Code: bash
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.

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.