File permissions: read, write and execute
Read the rwx permissions in ls -l, see why a script says Permission denied, and fix it with chmod u+x. Practise permissions safely in your browser.
Every file and directory says who may do what with it. ls -l shows it in the first column:
-rw-r--r-- 1 learner learner 128 Jan 12 09:00 hello.sh
The first character is the type (- file, d directory). The next nine are three groups of three:
| Characters | Who | Here |
|---|---|---|
rw- | user: the owner (learner) | read and write |
r-- | group: the owner's group | read only |
r-- | others: everyone else | read only |
In each group, r is read (look inside), w is write (change it) and x is execute (run it as a program; for a directory, go into it). A - means that permission is missing.
Running a script
A script is a text file of commands. To run one, you give its path, ./ meaning "in this directory":
cd scripts./hello.shThat fails with Permission denied: nobody has x yet.
chmod changes permissions
chmod (change mode) takes who (u, g, o or a for all), + to add or - to remove, and the permissions:
chmod u+x hello.shls -l hello.shNow the owner's group reads rwx and ./hello.sh runs. Other examples: chmod go-r notes.txt stops the group and others reading, chmod +x adds execute for everyone. Only a file's owner can change its permissions.
Your turn
Make scripts/hello.sh executable for its owner (you), then run it.
Your task
Make scripts/hello.sh executable for its owner (you), then run it.
- hello.sh is executable for its owner (not done yet)
- Run it (not done yet)
The checklist ticks itself off as you work in the terminal: any way that gets the result counts.
Hints come one at a time, then one way to do it. Try each before the next.
What this lesson uses, in one place.
ls -l- Long format: permissions, links, owner, group, size, date and name, one per line More on ls →
chmod u+x- Lets the owner run the file (u = user, + = add, x = execute) More on chmod →
./script- Runs a program or script from the directory you are in