Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
A humble offering, hoping to find a place where its perlishness won't be scorned. On another site, I got a little obsessed with this question I came across: How can I get sorted, human-readable output from du? It doesn't have a sort option, so you have to pass it to another program, like sort:
du | sort -nr
But if you want human-readable output, you can't do that, because sort can't sort it numerically. Others were offering shell solutions that invoked du on each item twice, once before being passed through sort, and once after for the human-readable verison. Wasteful! After finding solution in Awk, I wrote this "one-liner":
du -B1 | sort -nr | perl -e '@h=qw(b K M G);for(<>){($s,@f)=split/\s+/ +;$e=3;$e-- while(1024**$e>$s);$v=($s/(1024**$e));printf "%-8s%s\n",sp +rintf($v>=100?"%d%s":"%.1f%s",$v,$h[$e]),@f;}'
But then I realized it'd probably be better to use Perl for the sorting, and got this shorter result:
du -h | perl -e 'sub h{%h=(K=>10,M=>20,G=>30);($n,$u)=shift=~/([0-9.]+ +)(\D)/;return $n*2**$h{$u}}print sort{h($b)<=>h($a)}<>;'
Both of them return human-readable disk usage output, sorted largest item first:
4.4M . 3.6M ./colors 372K ./plugin 128K ./autoload 100K ./syntax 100K ./doc
I almost posted this to the 'Obfuscation' area, but I thought this would be more appropriate. I am prepared to do penance if I was mistaken.

Update: Thanks to hbm, grinder, and Roy Johnson, a significantly smaller version has been golfed (putting it here because it's buried around ^7 in the comments):
perl -e'%h=map{/.\s/;99**(ord$&&7)-$`,$_}`du -h`;die@h{sort%h}'

In reply to du -h, sorted by bellaire

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    [erix]: probably on holiday, the slacker
    erix is reduced to use a BROWSER. (remember those?)
    [Discipulus]: figuring.. i'm still just use the side pm chat..
    [erix]: (all this while karls guitar gently weeps...)
    [planetscape]: (invade Hungary)++ #I'm hungry
    [stonecolddevin]: i went too far down the irc rabbithole to hook this back up there
    [Discipulus]: .. yes erix until NodeReaper put jhon coltrane again

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (8)
    As of 2017-06-22 21:11 GMT
    Find Nodes?
      Voting Booth?
      How many monitors do you use while coding?

      Results (531 votes). Check out past polls.