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

In reply to 'ls' with octal permissions by oko1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.