[TAG] 2-cent Tip: Octal permissions in "ls"
Sivaraj S
ssantosh at fossix.org
Sun Mar 27 07:32:29 MSD 2011
On Sunday 27 March 2011 08:31 AM, Ben Okopnik wrote:
> On Sat, Mar 26, 2011 at 10:53:50PM -0400, Benjamin Okopnik wrote:
>> I've always wondered why "ls" doesn't just have this as an option. Got
>> tired of wondering, so I went ahead and wrote it.
> And _of course_ the second I send it off, I realize that I've sent the
> "2 versions back" version instead of the one I've been using. [sigh]
> Let's try this again.
>
> ```
> #!/usr/bin/perl -w
> # Created by Ben Okopnik on Sat Mar 26 19:00:46 EDT 2011
> use strict;
>
> if (@ARGV&&$ARGV[0] ne '-O'){ exec '/bin/ls', @ARGV } else { shift; }
>
> for (qx#/bin/ls @ARGV#){
> my ($t, $p, $r) = /^(.)([rwxsStT-]+)(\s+\d+\s+\w+.+)$/;
> print and next unless $p;
>
> my $out = 0;
> my %d = map {split//} qw/sx S- r4 w2 x1 -0/;
> $out += 01000 if $p =~ y/tT/x-/;
> $out += 02000 if $p =~ s/(s)(?=.{3})$/$d{$1}/i;
> $out += 04000 if $p =~ s/(s)(?=.{6})$/$d{$1}/i;
>
> $p =~ s/([rwx-])// and $out += $d{$1} * oct($_) for (100)x3, (10)x3, (1)x3;
>
> printf "[%s] %04o %s\n", $t, $out, $r;
> }
> '''
>
Hi Ben,
I am not sure whether I am doing this right, but the output doesn't show/print
the octal values of the permissions. if the ls version matters, it's version 8.5.
I suppose the first column in the permission should be in octal, right?
git $ octalls.pl -O -l
total 20
drwxr-xr-x. 6 santosh santosh 4096 Mar 14 08:59 bti
drwxr-xr-x. 5 santosh santosh 4096 Mar 15 22:46 gitolite-admin
drwxr-xr-x. 24 santosh santosh 4096 Mar 12 02:00 kernel-omap
drwxr-xr-x. 24 santosh santosh 4096 Mar 27 08:48 linux-2.6
drwxrwxr-x. 4 santosh santosh 4096 Jul 18 2010 tlock
git $ octalls.pl -O
bti
gitolite-admin
kernel-omap
linux-2.6
tlock
git $
Santosh
More information about the TAG
mailing list