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

Free lesson

Managing files from the command line

After this lesson: I can move around and manage files from a command line.

45 min lesson9 commandsEveryday Linux commands lab: the thirty commands engineers type from memory

Receipts on a server with no screen

A café's till saves every receipt as a file. The files live on a small server in the back room.

The owner asks you to copy Monday's receipts somewhere safe. The server has no screen and no mouse. You connect from a laptop and get one line to type on.

That line is all you need, once you know a few short commands.

By the end of this lesson

Note

I can move around and manage files from a command line.

Key words

WordWhat it meansExample
Command linea place where you type instructions instead of clicking
Commandone instruction, sent when you press Enterpwd
Foldera place that holds files and other folders
Paththe address of a file or folder/home/student/projects
Working directorythe folder you are in now
Home folderthe folder that belongs to you~

The cells students fill in are left blank in this preview.

Before you start

Note

Press Start the lab on the bar at the top. The lab is a whole Ubuntu machine of your own, so it takes a minute to start. Each Run button types its command into your terminal. Press the Runs in order, because each one carries on from the last.

A path is the address of a file

The command line is always "in" one folder, the working directory. pwd prints it.

A path that starts with / is a full path. It works from anywhere.

A path with no / at the start is a relative path. The machine looks for it inside the folder you are in. cd moves you to another folder. cd .. goes up one folder.

Worked example: where cd takes you

You are inYou typeYou end up in
/home/studentcd projects/home/student/projects
/home/student/projectscd docs/home/student/projects/docs
/home/student/projects/docscd ../home/student/projects
/home/student/projectscd /var/log/var/log
/var/logcd projectsAn error: no such folder here

The cells students fill in are left blank in this preview.

Which folder am I in?

Command
pwd

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

What is in this folder?

Command
ls

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Go down two folders

Command
cd projects/docs

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Where am I now?

Command
pwd

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think cd projects works from any folder. It only looks inside the folder you are in now. If you see "No such file or directory", run pwd first. Or type a full path that starts with /.

mkdir, cp and mv change files

mkdir -p makes a folder, and any missing folder above it. cp copies a file and leaves the original where it was. mv moves a file. Give it a new name in the same folder and it renames the file.

rm removes a file. A server has no bin, so a removed file is gone.

The first Run below takes you home again.

Go back to your home folder

Command
cd

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Make a folder for receipts

Command
mkdir -p shop/receipts

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Copy a file into it

Command
cp projects/docs/notes.txt shop/receipts/monday.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Rename the copy

Command
mv shop/receipts/monday.txt shop/receipts/tuesday.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Look inside the new folder

Command
ls shop/receipts

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Going further

Type these yourself. Make a folder old inside shop. Copy tuesday.txt into it with the new name wednesday.txt.

Then run ls -R shop. It lists a folder and every folder inside it. Say what it will print before you press Enter.

About this lab and its source: Everyday Linux commands lab: the thirty commands engineers type from memory

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