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 the shell

After this lesson: I can work with files and folders from a shell.

50 min lesson12 commandsEveryday Linux commands lab: the thirty commands engineers type from memory

Finding an invoice with no mouse

A shop's web server holds thousands of files. A colleague asks you to find last month's invoices on it.

The server has no desktop and no mouse. You have one window and a blinking cursor.

Most servers work this way. You move round them by typing short commands.

By the end of this lesson

Note

I can work with files and folders from a shell.

Key words

WordWhat it meansExample
Shellthe program that reads your commands and runs them
Paththe address of a file or folder/home/student/projects
Working directorythe folder you are in now
Redirectsend a command's answer into a file>>
Pipehand one command's answer to the next|
Modethe letters that say who may read, write or run a filerw-r--r--

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

A path is the address of a file

The shell is always "in" one folder. This is the working directory. Commands act on it unless you say otherwise.

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

A path without a / at the start is a relative path. The shell looks for it inside the working directory. Two dots, .., mean the folder above.

Worked example: where cd takes you

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

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

Where am I?

Command
pwd

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

What is here?

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/app

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

Look inside

Command
ls

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

Go home again

Command
cd

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. Run pwd first, or type a full path that starts with /.

mkdir, cp and mv change things

mkdir makes a folder. cp copies a file, and the original stays 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. On a server there is no bin, so a removed file is gone.

Run the next four in order.

Make a folder

Command
mkdir -p shopfiles

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 shopfiles/notes.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 shopfiles/notes.txt shopfiles/todo.txt

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

Check the folder

Command
ls shopfiles

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

Redirects and pipes join commands

A command prints its answer on the screen. A redirect sends the answer into a file instead. > replaces the file. >> adds to the end.

A pipe, |, hands the answer to another command. ls | wc -l lists names, then counts the lines.

Add a line to the file

Command
echo "Order 2043 shipped" >> shopfiles/todo.txt

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

Read the file

Command
cat shopfiles/todo.txt

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

Count the files in a folder

Command
ls projects/app | wc -l

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 > and >> do the same job. > throws away what the file held and starts again. >> keeps it and adds to the end. Use >> unless you mean to start again.

Worked example: a mode as three digits

LettersOwnerGroupOthersDigits
rw-r--r--rw- = 4+2 = 6r-- = 4r-- = 4644
rwxr-xr--rwx = 4+2+1 = 7r-x = 4+1 = 5r-- = 4754
rwx------rwx = 7--- = 0--- = 0700

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

One group of letters as a number

Code: python
# One group of three letters, worked out.
group = "r-x"
total = 0
if group[0] == "r":
    total = total + 4
if group[1] == "w":
    total = total + 2
if group[2] == "x":
    total = total + 1
print(total)
# Change group to "rw-" and run again.

Going further

Type these yourself. Make a folder invoices inside shopfiles. Make three empty files in it with one command: touch takes several names.

Then count them with one pipe, and use > to write the count into shopfiles/count.txt. Say what cat shopfiles/count.txt will print before you run it.

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.