Decimal to octal
| Decimal | Binary | Octal | Hex |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 1 | 1 | 1 | 1 |
| 8 | 1000 | 10 | 8 |
| 10 | 1010 | 12 | A |
| 15 | 1111 | 17 | F |
| 16 | 10000 | 20 | 10 |
| 64 | 1000000 | 100 | 40 |
| 100 | 1100100 | 144 | 64 |
| 255 | 11111111 | 377 | FF |
| 256 | 100000000 | 400 | 100 |
| 1024 | 10000000000 | 2000 | 400 |
| 65535 | 1111111111111111 | 177777 | FFFF |
Divide repeatedly by eight and read the remainders backwards. 493 gives 755. Alternatively convert to binary and group the bits into threes from the right: each group is one octal digit.
How to convert decimal to octal
Going via binary is usually faster than dividing by eight. Convert to binary, group the bits into threes from the right, and read each group: the grouping is exact because 8 is 2 cubed. This is the same trick that makes hex easy, with three bits instead of four, and it works for any base that is a power of two.
For permissions the digits line up with the bits directly: 493 is 755, which is 111 101 101: read, write and execute for the owner, read and execute for group and others. A fourth digit in front carries the special bits, 4 for setuid, 2 for setgid and 1 for sticky, which is why /tmp is 1777 and a setgid directory is 2775.
The leading zero is the trap. In C a bare 0755 means octal, and that convention survives in enough places to be dangerous; Python 3 and JavaScript in strict mode both reject 0755 outright and want 0o755. The version that bites hardest is passing a decimal number where octal was expected: chmod given the decimal 755 sets octal 1363, which is the sticky bit plus a permission set nobody intended.
Questions
755, which as Unix permissions is rwxr-xr-x.
Go via binary and group the bits into threes from the right.
0o in modern languages; a bare leading zero in C and its descendants. Python 3 and strict-mode JavaScript reject the bare zero.
The special bits. 4 setuid, 2 setgid, 1 sticky. 1777 is the sticky bit plus rwx for everyone, which is how /tmp is set.
It is read as octal 1363, a different permission set entirely. Whatever reaches chmod has to be octal already.
10: the same way 10 in decimal is the first two-digit number.
Mainly for Unix permissions. Hexadecimal has replaced it almost everywhere else.