Friday, September 27, 2013

CHMOD command and strange numbers

You might come across the below command like 

chmod 755 filename

and of course you will be wondering what this is. 

The thing is, that you can change the entire permission pattern of a file in one go using one number like the one in this example. Every mode has a corresponding code number, and as we shall see there is a very simple way to figure out what number corresponds to any mode. 

Every one of the three digits on the mode number corresponds to one of the three permission triplets. (u, g and o) Every permission bit in a triplet corresponds to a value: 
4 for r, 
2 for w, 
1 for x. 

If the permission bit you add this value to the number of the permission triplet. If it is cleared, then you add nothing.

So if a file has rwxr-xr-x permissions we do the following calculation: 
Triplet for u: rwx => 4 + 2 + 1 = 7
Triplet for g: r-x => 4 + 0 + 1 = 5
Triplet for o: r-x => 4 + 0 + 1 = 5

Which makes : 755 

Now i guess, you have solved one of the mysteries from your mind. :-)

No comments:

Post a Comment