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.
#!/usr/bin/perl
#===========================================================#
# What Chmod ? Simple Perl utility to convert a symbolic #
# chmod string (rwxr-x--x) into its octal notation (755) #
# Ozh - http://planetozh.com #
#===========================================================#
$arg = $ARGV[0];
# no argument or bad argument length
if (!$arg || length $arg < 9 || length $arg >10 ) {
print "Usage: $0 <pattern>\n";
print "Example: $0 rwxr-x--x\n";
exit;
}
# remove the filetype bit
if (length $arg > 9) {
$arg = substr($arg,1);
}
print $arg;
# o: owner, g: group, w:others (world)
($or,$ow,$ox,$gr,$gw,$gx,$wr,$ww,$wx) = split(//,$arg);
$o=0;
$g=0;
$w=0;
# Owner (or User) bit
if ($or eq "r") {$o += 4;}
if ($ow eq "w") {$o += 2;}
if ($ox eq "x") {$o += 1;}
# Setuid bit
if ($ox eq "S") {$s += 4;}
if ($ox eq "s") {$s += 4;$o += 1;}
# Group bit
if ($gr eq "r") {$g += 4;}
if ($gw eq "w") {$g += 2;}
if ($gx eq "x") {$g += 1;}
# Setgid bit
if ($gx eq "S") {$s += 2;}
if ($gx eq "s") {$s += 2;$g += 1;}
# Other (or World) bit
if ($wr eq "r") {$w += 4;}
if ($ww eq "w") {$w += 2;}
if ($wx eq "x") {$w += 1;}
# Sticky bit
if ($wx eq "T") {$s += 1;}
if ($wx eq "t") {$s += 1;$w += 1;}
print " = $s$o$g$w\n";
exit 1;Usage :
$ ./whatchmod rw--w--wT
rw--w--wT = 1622
#!/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 ??? ;)
for 10 character strings like drwxrwxrwx: