File Permissions and ‘chmod’

Since I work with Linux in both Advanced Programming (C) and in my DevTech course, I had to implement file permissions and ‘chmod’ my shell scripts. I was always a little confused on this, but I was finally able to understand how the permissions are displayed and how to change and why we need to chmod a shell script, so below is an explanation of this:

When you type the commands ls -l into terminal you can see all the files or directories in the directory you are in and you can see all the file permissions for each. An example of ls -l looks like this:

The format of the file permissions look like:

_ _ _ _ _ _ _ _ _ _

which is 10 fields which are known as its “file mode bits” which give the different permissions. 

You can also look at it like:

_    _ _ _    _ _ _   _ _ _ 

The first character stands for whether it is a directory or not. If it is a directory, it has a ‘d’ as the 1st character. If it is just a file or not a directory, it will just have a dash: ‘-‘.

The next parameters are grouped into groups of 3. The 1st group stands for the user permissions, the second group of stands for the group permissions, and the third group stands for other permissions

The 3 permissions are:

read – r

write – w

execute – x

(Pretty self-explanatory right?) 

So another way to view the file mode bits are:

d   usr:r w x   grp:r w x   othr:r w x 

Given that, lets now analyze the ls -l of my previous directory I showed in the image above: 

As you can see, the only row that has a ‘d’ as the first mode bit is my directory lecture-5 which is a separate “folder” that contains the lecture notes inside my current hw-5 directory (lecture-5 is also shown in purple to indicate it is a directory).

Now, lets look at 1 file’s permissions just to apply what we learned above: script.sh

The file permissions for script.sh are:

-rwxr–r–

Let’s put this into the format above as:

direc:–    usr:rwx   grp: r–   othr:r–

  1. Directory: Since directory bit mode is a dash, this means it is not a directory.
  2. User permissions: User has permissions to read, write, and execute.
  3. Group permissions: Group only has permission to read (the rest are indicated by dashes).
  4. Other permissions: Same as group, Other only has permission to read (the rest are indicated by dashes).

Now that we understand how to see the permissions on file, we’ll talk about how to change permissions with chmod.

Chmod Modifies File Permissions

First, we must understand read, write, and execute can be represented by these numbers:

read – 4

write – 2

execute – 1

The format of ‘chmod’ is 3 digits. For example, if I wanted to give file permissions to a script called script.sh, I could do:

chmod 744 script.sh

Now, let’s go over what permissions that gives. The format of the digits are:

usr      grp      othr

The summation of the read, write, and execute numbers are the permissions given to each of the 3 groups. So, given the digits 744, this means that:

  1. usr = 7 = 4 + 2 + 1  -> which is all the permissions ( read, write, execute)
  2. grp = 4 -> 4 is just the read permission
  3. othr = 4 -> 4 is just the read permission

**Note: the sum of the numbers 4, 2, 1 are unique and cannot be produced by other combinations and therefore can serve to indicate file permissions

So chmod 744 script.sh would give us the exact same permissions that we just observed on script.sh:

-rwxr–r–

Where the user has all permissions ( read, write, execute), and the group and other both only have read permissions.

Here are the other combinations you can have:

# Permission rwx Binary
7 read, write and execute rwx 111
6 read and write rw- 110
5 read and execute r-x 101
4 read only r– 100
3 write and execute -wx 011
2 write only -w- 010
1 execute only –x 001
0 none 000

 

 

 

 

 

 

2 comments on “File Permissions and ‘chmod’Add yours →

  1. Great post. I was checking constantly this blog and I am impressed! Extremely useful information specially the last part 🙂 I care for such info a lot. I was looking for this particular info for a very long time. Thank you and good luck.

  2. The first character stands for whether it is a directory or not. If it is a directory, it has a ‘d’ as the 1st character. If it is just a file or not a directory, it will just have a dash: ‘-‘.

Leave a Reply

Your email address will not be published.