Eren Akbulut's Blog

Unix Commands VI - chmod

January 30th, 2021

Hello everyone in today's we'll be taking look at Unix chmod command and we'll briefly cover it and explain some use over the examples, chmod is too much to cover in a post in my opinion not because it has lots of different use cases and examples, because it has a strange learning curve that you need to jump over before you can confidently say that you understood the deal.

So'll add additional links to the end so you can experiment with stuff yourself.


What is chmod?

"chmod" is a command that allows you to change file modes by changing the file mode bits. It's a widely used command on all Unix like operating systems and it'll be a part of your workflow when you are working on such a system.

Firstly we'll create 5 random files in a directory, then we can display their modes by typing the listing command "ls" with "-l" command.

unix-chmod-ls

We can see a bunch of stuff here but the thing that we need to pay attention on is the very left side and the right side mostly, the left side indicates the modes of the file while right is indicates the file name.

The mode in the left side gives us information about the ownership of the file, what do I mean by ownership, let me quickly explain it.

In Unix like operating systems we have 3 types of owner and in a file permissions can be set by the owner type explicitly. Those 3 types are User(Also known as owner), Group and the other.

  • User is the one that owns the file, by default it's the creator of the file.

  • Group is a group of users that have the same access level on a file given by the group.

  • Other is basically everyone else in this case neither created the file or somehow owns the file or in a Group.

Very left side of the mode declaration shows the permissions for the user/owner the middle part shows for the group and the right part shows for the others. So in our case our user can read and write but not execute while group and the other can only read the file.

Eventually we have 3 permission types to set and I'll explain how that works in a minute, from here on we can try to run some commands to change their modes. Firstly we'll run a command that'll give every type of permissions to each user and I'll iterate over how does numbers work afterwards.

chmod-unix-777

Here we run chmod with 777 on our ch1 file and as you can probably see all the file permissions are now set to allowed for all types of users now. Let's run some other on a different file and see what's new.

chmod-unix-755

Here we run chmod with 755 with command on ch2 this time and as you can see the permissions are different while user can still read write and execute, group and others can't write. But how exactly, let's quickly check the logic behind it.

unix-chmod-binary

Here we can see the letter representatives of the user permissions are actually corresponding to binaries, so as in our case "111101101" turns into "rwxr-xr-x" and to make any changes we can simply use the three number as we did above. You might also have seen such cases that people are using letter instead number like in the one below.

unix-chmod-letters

With letters we can add or subtract permissions from each file and we'll be doing that for the all ownership types basically so for example if you remember our basic ch3 file was just a file like ch4 and c5 back then, when we run chmod +x on it now all file types has the ability of execution on the file, if we are trying to remove a permission instead, we can just use it like the one in the second example when we remove readability from all ownership types.


There are many flags and stuff to do with chmod but I guess this much insight is enough for this tutorial, from the link here you can check the user manual of chmod to see other flags and from here you can play around with permissions interactively.

I hope to see you in the next time, take care :)

This blog has been created by Eren Akbulut 2020