pipr.tools

chmod-calc

Convert between numeric and symbolic chmod notation

⇔ Decode

Try it

stdin0 chars
stdout0 chars

Example

Convert between numeric and symbolic chmod

Usage
$ echo "755
644
rwxr-xr--" | chmod-calc
View source
(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");
    }

Suggested Pipelines

Related Tools