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:
Let’s put this into the format above as:
direc:– usr:rwx grp: r– othr:r–
- Directory: Since directory bit mode is a dash, this means it is not a directory.
- User permissions: User has permissions to read, write, and execute.
- Group permissions: Group only has permission to read (the rest are indicated by dashes).
- 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:
- usr = 7 = 4 + 2 + 1 -> which is all the permissions ( read, write, execute)
- grp = 4 -> 4 is just the read permission
- 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:
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:
||read, write and execute
||read and write
||read and execute
||write and execute