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.
cdShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
echo 'echo it works' > hello.shShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
ls -l hello.shShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
./hello.shShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
chmod +x hello.shShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
ls -l hello.shShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
./hello.shShown 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.
rm scratch/old.logShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
rm -rf scratch/cacheShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
ls scratchShown 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.
whoamiShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
id devShown 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.