In: , , ,
On: 2007 / 08 / 17
Shorter URL for this post: http://ozh.in/fh

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.

  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 :

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

Shorter URL

Want to share or tweet this post? Please use this short URL: http://ozh.in/fh

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.

2 Blablas

  1. zundog says:

    #!/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. hmpierson says:

    for 10 character strings like drwxrwxrwx:

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

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar and sign for a free account
Spam: Various spam plugins may be activated. I'll put pins in a Voodoo doll if you spam me.

Read more ?