Introduction
Welcome 👋 to this blog. In this blog we will gonna be learning about the chmod
a command-line utility on Unix/Linux systems. A command-line utility is a program or tool that you run using CLI(Command Line Interface) instead of GUI(graphical user interface)
chmod
chmod
stands for change mode. It's a command-line utility which used to change the permissions of files and directories. In Unix/Linux or in general computer Science we can define permission following:
read(r), view file content
write(w), modify file content
execute(x), run as a program
With the help of this command, we can change the permissions of the files assigned to three user groups.
In Unix/Linux systems we have three user classes:
Owner(u), user who owns a file
group(g), users in the file group
other(o), other users
Let's get our hands dirty with code
Open your terminal by pressing CTRL + Alt + T.
We have 2 ways to use this commands
1. With symbols
In this we will be using these symbols +
, -
and =
symbols to modify the permissions. The meaning of the symbols +
(Add permission), -
(Remove permission) and =
(set exact permissioin).
Let's start typing the command in the terminal:
I have created a three files in the blog
folder.
ankur@ankur:~/Desktop/blog$ ls
file1.txt file2.txt file3.txt
Let's check the permission of all the files by executing the ls
commands with -l
flag.
ankur@ankur:~/Desktop/blog$ ls -l
total 0
-rw-rw-r-- 1 ankur ankur 0 Jun 28 23:15 file1.txt
-rw-rw-r-- 1 ankur ankur 0 Jun 28 23:15 file2.txt
-rw-rw-r-- 1 ankur ankur 0 Jun 28 23:15 file3.txt
The -rw-rw-r--
part, which represents file permissions in Linux. This string has 10 characters, structured like this:
[Type][Owner][Group][Others]
- rw- rw- r--
- Give execute permission to the owner
ankur@ankur:~/Desktop/blog$ chmod u+x file1.txt
ankur@ankur:~/Desktop/blog$ ls -l
total 0
-rwxrw-r-- 1 ankur ankur 0 Jun 28 23:15 file1.txt
Now you can see the x in the owner string, which means it has execute privilege.
- Removing execute permission from the owner
ankur@ankur:~/Desktop/blog$ chmod u-x file1.txt
ankur@ankur:~/Desktop/blog$ ls -l
total 0
-rw-rw-r-- 1 ankur ankur 0 Jun 28 23:15 file1.txt
Now you can see the x vanishes in the owner string.
- Set exact permission explicitily
ankur@ankur:~/Desktop/blog$ chmod u=rwx,g=r,o=wr file1.txt
ankur@ankur:~/Desktop/blog$ ls -l
total 0
-rwxr--rw- 1 ankur ankur 0 Jun 28 23:15 file1.txt
You can see the string is changed.
2. With Numbers
Each permission is mapped to a numeric value.
r (read) 4
w (write) 2
x (execute) 1
So, everything worked the same expect in this method we passed the 3 string 1st one for owner, 2nd one for group and the 3rd one for others
for example
ankur@ankur:~/Desktop/blog$ chmod 755 file1.txt
ankur@ankur:~/Desktop/blog$ ls -l
total 0
-rwxr-xr-x 1 ankur ankur 0 Jun 28 23:15 file1.txt
755 will be breaked into(7, 5, 5)
7 -> 4+2+1 (rwx)
5 -> 4+1 (r-x)
5 -> 4+1 (r-x)
You can visit link to see the mapping of permission with the numeric.
🎉 You nailed it
You’ve now learned how to use the chmod command to change the permission of the files with two methods symbolic and numerical. It’s time to open your terminal and try these commands on your own system. Play around, explore, and see what's happening under the hood of your machine. Got something cool or unexpected? Share your best use case of chmod commands in the comment section.
Let's connect
📧 Email: ankursingh91002@gmail.com
🔗 LinkedIn: Ankur Singh
🔗 Twitter: @ankur_136
Top comments (0)