File permissions in UNIX are frequently specified as an octal number. Why is octal the preferred base for this purpose?
2 Answers
Because the permissions are stored in a bit field, grouped in 3-bit chunks (eg rwx).
Octal is a natural way to describe them because you only need 1 octal digit to exactly represent any sequence of 3 binary digits. (This is similar to why hexadecimal is used in programming -- since 1 hex digit can represent 4 binary digits, you only need 2 hex digits to describe 1 byte. It boils down to the fact that 8 and 16 are powers of 2, so converting between octal and binary or hexadecimal and binary is really easy.)
These chunks are user (aka owner), group and others. Each chunk has three permissions to describe: read, write, and execute.
Hence the full set of permissions looks something like rwxrwxrwx, where each is a binary digit. So, for example, 110100100 in binary becomes 644 in octal, and you know that the first octal digit is for the owner's permissions, the second for the group's permissions, and the third for everyone else.
-
Traditionally user, group and other (not owner). The alternate syntax for chmod would be "chmod u+x file", read as "allow the user to execute this file."– CarlFJan 31, 2010 at 6:20
-
fixed. pavium, ya beat me to the punch so i added the points i was going to make into your answer. good job. Jan 31, 2010 at 6:22
-
@~quack, thanks very much for the clarification and the comment. 'chunks' is a better word... makes more sense this way.– paviumJan 31, 2010 at 9:23
There are three different classes specified by permissions on an object: Owner, group, and everyone else. Each of these classes has three independent permissions that can be set: read, write, and execute. This means each class has one of eight permission sets:
- Execute
- Write
- Write/execute
- Read
- Read/execute
- Read/write
- Read/write/execute
- No permissions
Since an octal digit takes one of eight different values (the numbers 0 through 7), you can represent any combination of read/write/execute with a single octal digit.
Internally, the computer stores permissions as a bit field. So, for example, if you set permissions to 644, the computer might represent this as the binary number 110100100, or:
110 | 100 | 100
rw- | r-- | r--
6 | 4 | 4
Owner | Group | World
(Notice that binary 110 is equal to octal 6, and binary 100 is equal to octal 4).
-
the eight possible settings is true, but incidental. we don't really consider all the different possibilities there -- the system sees it as a field of independent bits, and adds the bits together only when converting to an octal representation. Jan 31, 2010 at 6:26
-
This is true, but I figured mapping bits might be a little tougher to understand for someone not already familiar with it. I did add it below. Jan 31, 2010 at 6:28