Skip to the workspace
Pebellslearn by building
Working with Files  ›  Lesson

File Permissions — Who May Read, Write and Run

File Permissions 15 minEach file has read, write and execute switches for its owner, its group and everyone else, set with chmod using letters or numbers where read is 4, write is 2 and execute is 1.

linux / File Permissions — Who May Read, Write and Run

Explore each scene at your own pace.

01 · The rule

The rule

File Permissions — Who May Read, Write and Run

Asha wants the whole garden club to read harvest.csv, only herself to change it, and her harvest-report script to be runnable. On Linux, every file carries a small set of switches that decide exactly this, and ls -l shows them to you.

The rule

Three people, three switches: read 4, write 2, execute 1.

$ ls -l ~/garden
-rw-r--r-- 1 asha garden  181 Jun  4 09:12 harvest.csv
-rwxr-x--- 1 asha garden  420 Jun  4 09:30 harvest-report
drwxr-x--- 2 asha garden 4096 Jun  4 09:00 plots

Read the first column in chunks: one character for the type, then three groups of three.

-   rw-   r--   r--
│   │     │     └── others (everyone else)
│   │     └──────── group (the file's group: garden)
│   └────────────── user (the owner: asha)
└────────────────── type: - file, d directory, l link

Each group has three switches: r (read), w (write) and x (execute), or a - if the switch is off.

For directories the letters mean something slightly different:

  • r lets you list the names inside.
  • w lets you create, delete and rename entries inside.
  • x lets you enter the directory and reach the files in it.

Changing permissions with chmod, the symbolic way (who, then +, - or =, then the letters):

$ chmod u+x harvest-report     # owner may now run it
$ chmod go-w harvest.csv       # group and others lose write
$ chmod o= plots               # others get nothing

The number way uses one digit per group, adding read 4, write 2 and execute 1:

$ chmod 644 harvest.csv        # rw- r-- r--
$ chmod 750 harvest-report     # rwx r-x ---

The two styles suit different jobs. Letters are best for a small change to what's already there ("also let the owner run it"). Numbers set all nine switches at once, so they're best when you know exactly what the final result should be. Note that chmod only works on files you own, unless you use sudo.

To change who owns a file, use chown, which normally needs root: sudo chown asha:garden harvest.csv.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.