Skip to content

Instantly share code, notes, and snippets.

@pokle
Created May 29, 2010 01:53
Show Gist options
  • Save pokle/417956 to your computer and use it in GitHub Desktop.
Save pokle/417956 to your computer and use it in GitHub Desktop.
Sort the output of du -h
#!/usr/bin/perl
#
# Sorts the output of du -h
use strict;
my $multip = {
'G' => 1024*1024*1024,
'M' => 1024*1024,
'K' => 1024,
'B' => 1
};
my $data = [];
while(<>) {
my $line = $_;
if ($line =~ m/^\W*([\d\.]+)([GMKB])\W+(.*)$/) {
my $bytes = $1 * $$multip{$2};
push @$data, [$bytes, $line];
} else {
print "? $line";
}
}
my @sorted = sort {
$$a[0] <=> $$b[0]
} @$data;
foreach my $item (@sorted) {
print $$item[1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment