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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |