Well, heck. One of our Great and Holy Brethren, jdporter, took a shot at rewriting 'cut'... I can be brave too. :) Well, not all _that_ brave - I'm not rewriting "ls" from scratch, thankyouverymuch - but I've always wondered why it doesn't have an option for displaying permissions in octal. Well, now it does.

I've kept the formatting and all the other goodies of "ls" - you can still specify all the same options, etc. (not that I've done a huge amount of testing; I'm sure there are edge cases that would break it - I just haven't managed to come up with any.) I also suspect that there are better, smarter ways to do this, so comments would be very welcome.

Anyway, here it is. Enjoy.

#!/usr/bin/perl -w # Created by Ben Okopnik on Sat Mar 26 19:00:46 EDT 2011 use strict; for my $ls (qx#/bin/ls -l @ARGV#){ my ($t, $p, $r) = $ls =~ /^(.)([rwxsStT-]+)(\s+\d+\s+\w+.+)$/; print $ls 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; for ((100) x 3, (10) x 3, (1) x 3){ $out += $d{$1} * oct($_) if $p =~ s/([rwx-])//; } printf "[%s] %04o %s\n", $t, $out, $r; }
-- 
Education is not the filling of a pail, but the lighting of a fire.
 -- W. B. Yeats

Replies are listed 'Best First'.
Re: 'ls' with octal permissions
by jwkrahn (Abbot) on Mar 27, 2011 at 03:43 UTC
    my %d = map {split//} qw/sx S- r4 w2 x1 -0/;

    Really???    Why not just:

    my %d = qw/s x S - r 4 w 2 x 1 - 0/;


    Or you could just do the whole thing like this:

    for my $file ( @ARGV ) { my $mode = ( stat $file )[ 2 ]; my $ls = `/bin/ls -l $file`; $ls =~ s/^(.)([rwxsStT-]+)(?=\s)/[$1] @{[ sprintf '%04o ', $mo +de & 07777 ]}/; print $ls; }

      > Why not just [...]

      Because TMTOWTDI. :) Not a lot of cost to it, so no real difference. Besides I like the visual pairing of the sets.

      On the other hand, your suggested solution invokes 'ls' and 'stat' for every single file, and takes away the ability to use all the other 'ls' options (at which point it would be easier to use a shell script.) That's exactly what I wanted to avoid. I actually use this as a sort of a 'wrapper' around 'ls' - a drop-in replacement - in which I use the '-O' switch (which I shift off) to enable this behavior; otherwise, I just pass everything to 'ls'.

      -- 
      Education is not the filling of a pail, but the lighting of a fire.
       -- W. B. Yeats
Re: 'ls' with octal permissions
by owill (Initiate) on Jun 06, 2012 at 14:46 UTC

    I just wrote a simular but far less elegent version of this yesterday because I didn't find this first.

    However when I tested this against my test cases I did find some edge cases where it fails.

    This file:
    -rwS--S--- 2 will will 0 Jun 5 11:37 foo.bar
    Should be mode 6600, but this code returns 0600.
    This one:
    -rwS--S--T 1 will will 0 Jun 5 11:37 foo.7600
    Should be 7600, but this code returns 1600

    I'm not sure I've every seen files with these permissions in the wild, but still..

    Nice code though.