In: , , ,
On: 2007 / 08 / 17 Viewed: 32212 times

Have you ever "ls -l" a file or directory, watch its "-r-x--s-wT" symbolic notation and ask to yourself: "what the hell is that chmod ?". 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 stinky ---xr-s-wT into the octal 3152 value.

PERL:
  1. #!/usr/bin/perl
  2. #===========================================================#
  3. # What Chmod ? Simple Perl utility to convert a symbolic    #
  4. # chmod string (rwxr-x--x) into its octal notation (755)    #
  5. # Ozh - http://planetozh.com                                #
  6. #===========================================================#
  7.  
  8. $arg = $ARGV[0];
  9.  
  10. # no argument or bad argument length
  11. if (!$arg || length $arg <9 || length $arg>10 ) {
  12.         print "Usage: $0 <pattern>\n";
  13.         print "Example: $0 rwxr-x--x\n";
  14.         exit;
  15. }
  16.  
  17. # remove the filetype bit
  18. if (length $arg> 9) {
  19.         $arg = substr($arg,1);
  20. }
  21.  
  22. print $arg;
  23.  
  24. # o: owner, g: group, w:others (world)
  25. ($or,$ow,$ox,$gr,$gw,$gx,$wr,$ww,$wx) = split(//,$arg);
  26.  
  27. $o=0;
  28. $g=0;
  29. $w=0;
  30.  
  31. # Owner (or User) bit
  32. if ($or eq "r") {$o += 4;}
  33. if ($ow eq "w") {$o += 2;}
  34. if ($ox eq "x") {$o += 1;}
  35. # Setuid bit
  36. if ($ox eq "S") {$s += 4;}
  37. if ($ox eq "s") {$s += 4;$o += 1;}
  38.  
  39. # Group bit
  40. if ($gr eq "r") {$g += 4;}
  41. if ($gw eq "w") {$g += 2;}
  42. if ($gx eq "x") {$g += 1;}
  43. # Setgid bit
  44. if ($gx eq "S") {$s += 2;}
  45. if ($gx eq "s") {$s += 2;$g += 1;}
  46.  
  47. # Other (or World) bit
  48. if ($wr eq "r") {$w += 4;}
  49. if ($ww eq "w") {$w += 2;}
  50. if ($wx eq "x") {$w += 1;}
  51. # Sticky bit
  52. if ($wx eq "T") {$s += 1;}
  53. if ($wx eq "t") {$s += 1;$w += 1;}
  54.  
  55. print " = $s$o$g$w\n";
  56.  
  57. exit 1;

Usage :

CODE:
  1. $ ./whatchmod rw--w--wT
  2. rw--w--wT = 1622

Related posts

Metastuff

This entry "What Chmod ? Symbolic to Octal Notation Perl Script" was posted on 17/08/2007 at 11:58 am and is tagged with , , ,
Watch this discussion : Comments RSS 2.0. You can trackback this post from your own site

2 Blablas

  1. 1
    zundog France »
    said, on 17/Dec/07 at 3:39 am # :

    #!/bin/bash

    stat $1 | sed -n -e 's|Access: (\([0-7]\{4\}\).*|\1|p;d'

    # why always use perl when you can do it simpler in bash ??? ;)

  2. 2
    hmpierson United States »
    said, on 05/May/08 at 2:43 am # :

    for 10 character strings like drwxrwxrwx:

    CODE:
    1. $_ = $ARGV[0];
    2.   tr/rwx-/1110/;
    3.    /^(.)(...)(...)(...)$/;
    4.   $chmod =  oct("0b$2") . oct("0b$3") . oct("0b$4");

Leave a Reply

Comment Guidelines or Die

  • HTML: You can use these tags: <a href=""> <em> <i> <b> <strong> <blockquote>
  • Posting code: Post raw code (no <> &lt; etc) within appropriate tags : [php][/php], [css][/css], [html][/html], [js][/js], [sql][/sql], [xml][/xml], or generic [code][code]
  • Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar.
  • Spam: Various spam plugins on patrol. I'll put pins in a Voodoo doll if you spam me.
  • I will mark as Spam test comments, all comments with SEO names (ie "My Cool Online Shop" instead of "Joe") or containing forum-like signatures.

Read more ?

Close
E-mail It