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

Free lesson

Everyday Linux 2: Permissions and removing

The whole lesson is here to read. Create a free account to run it in a live lab.

Virtual machine lab11 min read12 commands

The script that will not run

You write a small script, type its name and the machine answers: permission denied. You wrote it and it is yours. The machine still refuses, because on Linux every file says who may read it, who may change it and who may run it.

By the end of this lesson

Note

I can change who may run a file and delete files carefully.

Key words

  • Permission: who may read, change or run a file.
  • Owner: the user a file belongs to.
  • Group: a named list of users who share some files.
  • Script: a file of commands that the machine runs for you.

Starter: what you already know

Note

Answer in your head. 1. Which command prints the folder you are in? 2. Which sign puts words into a file, replacing what was there? 3. What is the difference between a file and a folder?

Before you start

Note

Press Start the lab on the bar at the top of this lesson. This lab is a whole Ubuntu machine of your own, so it takes a little longer to come up than a container. Each Run button types its command into the machine's terminal, one after another in the same session. If one says the machine is still starting, wait a few seconds and press it again. Where the lesson says Type this, the command never ends by itself or asks you something, so press Open terminal on the bar and type it yourself.

A new file cannot run yet

The first column of ls -l is the permissions. After the first character come three groups of rwx: what the owner, the group and everyone else may read (r), write (w) and run (x). A new file has no x, so the first ./hello.sh is refused. chmod +x adds the right to run it, and the second try prints it works.

Command
cd

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

Command
echo 'echo it works' > hello.sh

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

Command
ls -l hello.sh

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

Command
./hello.sh

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

Command
chmod +x hello.sh

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

Command
ls -l hello.sh

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

Command
./hello.sh

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

Check

Tip

In -rw-r--r--, who may write to the file? Answer: only the owner. The group and everyone else may read it.

Remove what you do not need

rm removes a file. rm -rf removes a folder and everything inside it, and does not ask. The folder scratch was made for you to delete from, and the last command prints nothing because it is empty now.

Command
rm scratch/old.log

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

Command
rm -rf scratch/cache

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

Command
ls scratch

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 a deleted file goes to a bin and can be fetched back, as on a phone or a desktop. On a server rm is final: the file is gone. Before rm -rf, run pwd and ls and read the path twice. rm -rf with the wrong path, or with a space in the wrong place, has deleted whole servers.

Who am I, and who else is here?

whoami prints your user name. id dev shows another account on this machine, which later lessons use. Every account has its own files, and permissions decide what each may touch.

Command
whoami

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

Command
id dev

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

Exit check

Tip

Answer without looking back. 1. Which command lets a file run? 2. What does rwx stand for? 3. Why check with pwd before rm -rf? Answers: chmod +x; read, write, execute (run); there is no bin, so a wrong path cannot be undone.

For teachers

Tip

This lesson is a small example of a lesson worked in a lab: the steps and the Run buttons are the lesson, and the lab is each student's own copy. This lesson is worked in a VM lab: each student has an Ubuntu machine of their own, and a Run types into its terminal and shows what it printed. Key learning points: 1. Permissions say who may read, write and run a file, and chmod +x lets it run. 2. rm is final, so check the path first. Common mistake: thinking rm has a bin. Equipment: the lab and a browser. Tip: make students read the path aloud before they press Enter on rm -rf. The lab has no check and marks nothing; set a hand-in of your own if you want one. Copy it into your own module and change it, or build one from scratch with the Lab menu in the lesson builder.

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

Teaching a class? Set these lessons for your students with marking and a lab for every student.