Hi All, Here's a script I have had to write a few times. I figured I'd post incase I lose it again. Basically it sorts the output of "du -sH". I use the script when I want to find out what directories are hogging up disk space.
#!/usr/bin/perl -w use strict; my %byFile; sub sortBySize { my $a_units = $byFile{$a} || 'x'; my $b_units = $byFile{$b} || 'x'; my $a_value = $byFile{$a}; my $b_value = $byFile{$b}; $a_units =~ s/\d+[\.\d+]+([k|kB|MB|GB])/$1/g; $b_units =~ s/\d+[\.\d+]+([k|kB|MB|GB])/$1/g; $a_value =~ s/$a_units//g; $b_value =~ s/$b_units//g; if ( $a_units eq $b_units ) { my $v = $a_value <=> $b_value; return $v; } elsif ( ($a_units eq "k" or $a_units eq "kB") and ($b_units +eq "k" or $b_units eq "kB") ) { my $v = $a_value <=> $b_value; return $v; } elsif ( $a_units eq "GB" ) { return 1; } elsif ( $b_units eq "GB" ) { return -1; } elsif ( $a_units eq "MB" ) { return 1; } elsif ( $b_units eq "MB" ) { return -1; } elsif ( $a_units eq "kB" or $a_units eq "k" ) { return 1; } elsif ( $b_units eq "kB" or $a_units eq "k" ) { return -1; } else { return 0; } } open ( DUSH, "du -sH *|") or die __FILE__ . "[" . __LINE__ . "] Can't +execute du -SH *:$!\n"; for (<DUSH>) { my ( $size, $name ) = split /\s+/; $byFile{$name} = $size; } for ( reverse sort sortBySize keys %byFile ) { print "$_\t=>$byFile{$_}\n"; }
Please post any improvements. Thanks!

Plankton: 1% Evil, 99% Hot Gas.

In reply to sort du -sH output to find that disk hog by Plankton

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.