Convert between numeric and symbolic chmod notation
⇔ DecodeConvert between numeric and symbolic chmod
$ echo "755
644
rwxr-xr--" | chmod-calc (input) => {
return input
.trim()
.split("\n")
.map((line) => {
line = line.trim();
if (!line) return "";
if (/^\d{3,4}$/.test(line)) {
const digits = line.slice(-3);
const sym = [...digits]
.map((d) => {
const n = parseInt(d);
return (
(n & 4 ? "r" : "-") +
(n & 2 ? "w" : "-") +
(n & 1 ? "x" : "-")
);
})
.join("");
return `${line} \u2192 ${sym}`;
}
if (/^[rwx-]{9}$/.test(line)) {
const num = [0, 3, 6]
.map((i) => {
const c = line.slice(i, i + 3);
return (
(c[0] === "r" ? 4 : 0) +
(c[1] === "w" ? 2 : 0) +
(c[2] === "x" ? 1 : 0)
);
})
.join("");
return `${line} \u2192 ${num}`;
}
return `${line} \u2192 Error: expected 3-digit octal or 9-char rwx string`;
})
.join("\n");
}