Skip to content

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.

Lesson 10 of 167 minSkip to the terminal ↓

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 120 Jan 12 09:00 hello.sh

The first character is the type (- file, d directory). The next nine are three groups of three:

CharactersWhoHere
rw-user: the owner (learner)read and write
r--group: the owner's groupread only
r--others: everyone elseread 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.sh

That 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.sh
ls -l hello.sh

Now 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 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)

Lesson 10 of 16.

learner@sandbox: ~bash (practice)
Note: Practice shell for this lesson. Type a command and press Enter (help lists them all).