{"id":665,"date":"2007-08-17T11:58:21","date_gmt":"2007-08-17T09:58:21","guid":{"rendered":"http:\/\/planetozh.com\/blog\/2007\/08\/what-chmod-symbolic-to-octal-notation-perl-script\/"},"modified":"2007-08-17T11:58:21","modified_gmt":"2007-08-17T09:58:21","slug":"what-chmod-symbolic-to-octal-notation-perl-script","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2007\/08\/what-chmod-symbolic-to-octal-notation-perl-script\/","title":{"rendered":"What Chmod ? Symbolic to Octal Notation Perl Script"},"content":{"rendered":"<p>Have you ever &quot;<em>ls -l<\/em>&quot; a file or directory, watch its &quot;-r-x&#8211;s-wT&quot; symbolic notation and ask to yourself: &quot;what the hell is that chmod ?&quot;. This was the case for me several times today, so before I spent 1 minute on thinking about the notation, I decided to spent 5 minutes on a quick Perl script. <strong>What Chmod<\/strong> converts those stinky <em>&#8212;xr-s-wT<\/em> into the octal <em>3152<\/em> value.<br \/>\n<!--more--><\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-1\"><pre class=\"language-perl line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-perl\">#!\/usr\/bin\/perl\r\n#===========================================================#\r\n# What Chmod ? Simple Perl utility to convert a symbolic    #\r\n# chmod string (rwxr-x--x) into its octal notation (755)    #\r\n# Ozh - http:\/\/planetozh.com                                #\r\n#===========================================================#\r\n\r\n$arg = $ARGV[0];\r\n\r\n# no argument or bad argument length\r\nif (!$arg || length $arg &lt; 9 || length $arg &gt;10 ) {\r\n        print &quot;Usage: $0 &lt;pattern&gt;\\n&quot;;\r\n        print &quot;Example: $0 rwxr-x--x\\n&quot;;\r\n        exit;\r\n}\r\n\r\n# remove the filetype bit\r\nif (length $arg &gt; 9) {\r\n        $arg = substr($arg,1);\r\n}\r\n\r\nprint $arg;\r\n\r\n# o: owner, g: group, w:others (world)\r\n($or,$ow,$ox,$gr,$gw,$gx,$wr,$ww,$wx) = split(\/\/,$arg);\r\n\r\n$o=0;\r\n$g=0;\r\n$w=0;\r\n\r\n# Owner (or User) bit\r\nif ($or eq &quot;r&quot;) {$o += 4;}\r\nif ($ow eq &quot;w&quot;) {$o += 2;}\r\nif ($ox eq &quot;x&quot;) {$o += 1;}\r\n# Setuid bit\r\nif ($ox eq &quot;S&quot;) {$s += 4;}\r\nif ($ox eq &quot;s&quot;) {$s += 4;$o += 1;}\r\n\r\n# Group bit\r\nif ($gr eq &quot;r&quot;) {$g += 4;}\r\nif ($gw eq &quot;w&quot;) {$g += 2;}\r\nif ($gx eq &quot;x&quot;) {$g += 1;}\r\n# Setgid bit\r\nif ($gx eq &quot;S&quot;) {$s += 2;}\r\nif ($gx eq &quot;s&quot;) {$s += 2;$g += 1;}\r\n\r\n# Other (or World) bit\r\nif ($wr eq &quot;r&quot;) {$w += 4;}\r\nif ($ww eq &quot;w&quot;) {$w += 2;}\r\nif ($wx eq &quot;x&quot;) {$w += 1;}\r\n# Sticky bit\r\nif ($wx eq &quot;T&quot;) {$s += 1;}\r\nif ($wx eq &quot;t&quot;) {$s += 1;$w += 1;}\r\n\r\nprint &quot; = $s$o$g$w\\n&quot;;\r\n\r\nexit 1;<\/code><\/pre><\/div>\n<p>Usage :<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-2\"><pre class=\"language-none line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-none\">$ .\/whatchmod rw--w--wT\r\nrw--w--wT = 1622<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever &quot;ls -l&quot; a file or directory, watch its &quot;-r-x&#8211;s-wT&quot; symbolic notation and ask to yourself: &quot;what the hell is that chmod ?&quot;. This was the case for me several times today, so before I spent 1 minute on thinking about the notation, I decided to spent 5 minutes on a quick Perl script. What Chmod converts those\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[235,2,3,9],"class_list":["post-665","post","type-post","status-publish","format-standard","hentry","tag-chmod","tag-code","tag-linux","tag-perl"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/665","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=665"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/665\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}