Everyone half-remembers the magic numbers — 644 for files, 755 for scripts, 600 for keys — without ever seeing that the digit is nothing more than 4 + 2 + 1. I built a chmod / Unix permissions calculator that makes all four representations one thing: tick a 3×3 grid and the octal, the rwxr-xr-x symbolic string, the chmod command and the ls -l line all move together — and it works backwards too, so type 750 or paste rwxr-x--- and the grid ticks itself. It's all real bit arithmetic in the browser, no lookup table. Here's the math underneath.
Three audiences, three permissions
Every file splits the world into three audiences — the owner (user), the owning group, and everyone else (other) — and each gets three permissions: read, write, execute. That's nine bits, laid out as user's rwx, then group's, then other's. Plus three special bits. Once you see it as a 3×3 grid there's nothing left to memorise; you're just ticking boxes. On a file those letters mean view / modify / run-as-a-program; on a directory they mean list-names / add-remove-entries / enter-and-traverse — the twist that trips everyone up, because a directory you can read but not execute shows you names you can't actually open.
Each permission is a bit worth 4, 2 or 1
Here's the trick that turns permissions into arithmetic. The three permissions of one audience are the three bits of a single octal digit: read is the 4s bit (100), write the 2s bit (010), execute the 1s bit (001). Any combination is just a sum, so composing one class is a bitwise OR:
function classToDigit(c){
return (c.r ? 4 : 0) | (c.w ? 2 : 0) | (c.x ? 1 : 0);
}
// rwx -> 4|2|1 = 7 · r-x -> 4|1 = 5 · rw- -> 4|2 = 6
Do that for each audience, line up the three digits, and that number is the mode. rwxr-xr-x → 7 5 5 → 755. The famous numbers aren't codes handed down from on high; they're three little sums written side by side.
Parsing is the exact inverse — AND instead of OR
The reverse direction is where "real bit math" earns its keep. To pull a digit apart you AND it against 4/2/1:
function digitToClass(d){
return { r: !!(d & 4), w: !!(d & 2), x: !!(d & 1) };
}
// 7 & 4 is truthy -> read on · 5 & 2 is 0 -> write off
Because compose (OR) and parse (AND) are exact inverses, a single source-of-truth mode object plus one render() keeps the grid, the octal box and the symbolic box in perfect agreement — type into any one and the rest follow, and they cannot disagree.
The special bits are the leading 4th digit
Beyond the nine ordinary bits are three special bits, written as a fourth digit in front: setuid (4), setgid (2), sticky (1). setuid on an executable makes it run as its owner — that's how passwd edits a root-owned file; setgid on a directory makes new files inherit the directory's group; the sticky bit on world-writable /tmp stops users deleting each other's files. So 4755, 2775, 1777 are just an ordinary mode with one switch flipped. They don't get their own column — they hijack the execute slot of the relevant audience, showing as s/t when execute is on and upper-case S/T when it's off, so rwsr-xr-x is 4755 and reading the case tells you whether execute is really there.
Five things people get wrong
The calculator ends on the gotchas: 777 lets anyone on the machine write the file (almost never what you want); execute on a directory ≠ execute on a file; the leading digit is the special bits; and — the subtle one — numeric mode is absolute (chmod 644 f sets it exactly) while symbolic mode is relative (chmod u+x f only adds one bit to whatever's there). The moment you internalise 4+2+1, the magic numbers stop being magic and you can read or write any Unix permission by hand.
Tick the grid and watch every field sync (backwards too):
https://dev48v.infy.uk/solve/day44-chmod-calculator.html
Top comments (0)