#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Getopt::Std; my %opts = (); getopts('d:rh', \%opts); print usage() if(!defined $opts{'d'} or $opts{'h'}); my @du = qx(du -h --max-depth=1 $opts{'d'}); my %d = (); foreach(@du) { chomp; if(/(\d+)K(.*)$/) { $_ = ($1 *1024)." ".$2; } if(/(\d+)M(.*)$/) { $_ = ($1 *1024*1024)." ".$2; } if(/(\d+)G(.*)$/) { $_ = ($1*1024*1024*1024)." ".$2; } if(/(\d+) (.*)$/) { push @{$d{$1}}, $2; } } @du = (); foreach (sort {$a <=> $b} keys %d) { my $key = $_; if ($_ > 1073741824) { $key = ($key / 1073741824)."G"; } elsif($_ > 1048576) { $key = ($key / 1048576) ."M"; } elsif($_ > 1024) { $key = ($key / 1024) ."K"; } foreach my $ele (@{$d{$_}}) { push @du, $key."\t".$ele; } } if($opts{'r'}) { print $_."\n" foreach(reverse @du); } else { print $_."\n" foreach(@du); } sub usage { print< [-r] Arguments: mandatory: -d directory name optional: -r if the sort order should be descending EOF exit 0; }