Linux Files Permissions

What is File Permission?

Working on file permission is weird when you start using Linux as an operation system. Imagining have a project folder and you want to change permission to be able to execute the script 🥲

First, when you run ls -al on your project folder 📂, you should see something like this:

total 20
drwxrwxr-x 4 ubuntu ubuntu 4096 Aug 23 07:40 .
drwxr-xr-x 18 ubuntu ubuntu 4096 Aug 23 07:40 ..
drwxrwxr-x 7 ubuntu ubuntu 4096 Aug 23 07:38 .git
-rw-rw-r-- 1 ubuntu ubuntu 0 Aug 23 07:38
-rw-rw-r-- 1 ubuntu ubuntu 0 Aug 23 07:39 data.csv
drwxrwxr-x 2 ubuntu ubuntu 4096 Aug 23 07:40 docs
-rw-rw-r-- 1 ubuntu ubuntu 0 Aug 23 07:39
-rwxrwxr-x 1 ubuntu ubuntu 28 Aug 23 07:40

ls is for list -a All (hidden files) -l (long list with more details). If you want more useful tool check exa.

The first column shows us for each file/folder its file permission. To understand it, you should know that each file in Linux have a permission for Group, User and Other (Applications, guest users,… etc)

This value drwxrwxr-x is the presentation of a file persmission for a folder (d: folder, -: filer) then, you should devide it by 3, then we have this :

(d: Folder / rwx: Group / rwx: User / r-x: Other)

r = 👁️ Read permission
w = ✍️Write permission
x = 🐎 eXecute permission
– = 🙅no permission

You should understand Users in Linux to understand Group and Users. In Linux, all users are listed in a file /etc/shadow.
This file contains lines of each users created with this synthax:

This is an example:
[username]:[encrypted password]:[UID]:[GID]:[GECOS]:[home_dir]:[shell_path]

That means, each user belongs to a group.

How to update file permission?

Permissions values are R: read, W: write, X: execute. These values are stocked in Octal value.

Octal counting is based on 3 bits from 0 to 8 digits only.

0: 000 <- ---
1: 001 <- --x
2: 010 <- -w-
3: 011 <- -wx
4: 100 <- r--
5: 101 <- r-x
6: 110 <- rw-
7: 111 <- rwx

If we check the file permission, it’s permission is -rwxrwxr-x, now we understand that it means (-: file, full access to Group and user, only read and execute to other so no one can write on it)

To change the file permission we need to use the command line is chmod, then we pass the octal value.
For example, we need to change the permission to full access (Don’t use it in real life !):

May be a cartoon of text
$ chmod -R 777 docs

This command will add full access to anyone to read, write and execute files in this folder, so anyone can put a script and execute it.

This post is inspired by this tweet:

You Might Also Like

Leave a Reply