Article View: comp.unix.shell
Article #267Re: sort du output
From: Tom Christiansen
Date: Mon, 03 Aug 1992 03:31
Date: Mon, 03 Aug 1992 03:31
94 lines
3369 bytes
3369 bytes
From the keyboard of wongm@latcs1.lat.oz.au (/home/pgrds/wongm):
:Hello,
: I have a simple question to ask, ie when using du command, how can
: can we have the listing sorted with the file size as sort key.
:
: I tried : du | +0 -1 but this will not give correct result,
: since if we have :
:
: file size
: ---- ----
: A 10
: B 2
: C 3
:
: The outout from the pipeline is "
:
: 10 ./A
: 2 ./B
: 3 ./C
:
: How can we do it correctly, do we have to pipe the field 0 from
: du to bc or some similar command to do that ?
While you can pipe into a numeric sort, I think you may not like the
output, since it's unclear how much space a directory takes up compared to
its contents (or a file, if you use -a). I happen to find that my dutree
program yeids output in a useful order.
#!/usr/bin/perl
# dutree -- tchrist@convex.com
@lines = `du @ARGV`;
chop(@lines);
&input($top = pop @lines);
&output($top);
exit;
sub input {
local($root, *kid, $him) = @_[0,0];
while (@lines && &childof($root, $lines[$#lines])) {
&input($him = pop(@lines));
push(@kid, $him);
}
if (@kid) {
local($mysize) = ($root =~ /^(\d+)/);
for (@kid) { $mysize -= (/^(\d+)/)[0]; }
push(@kid, "$mysize .") if $size != $mysize;
}
@kid = &sizesort(*kid);
}
sub output {
local($root, *kid, $prefix) = @_[0,0,1];
local($size, $path) = split(' ', $root);
$path =~ s!.*/!!;
$line = sprintf("%${width}d %s", $size, $path);
print $prefix, $line, "\n";
$prefix .= $line;
$prefix =~ s/\d /| /;
$prefix =~ s/[^|]/ /g;
local($width) = $kid[0] =~ /(\d+)/ && length("$1");
for (@kid) { &output($_, $prefix); };
}
sub sizesort {
local(*list, @index) = shift;
sub bynum { $index[$b] <=> $index[$a]; }
for (@list) { push(@index, /(\d+)/); }
@list[sort bynum 0..$#list];
}
sub childof {
local(@pair) = @_;
for (@pair) { s/^\d+\s+//g;
s/$/\//; } ### added line
#Date: Tue, 19 May 92 15:35:24 EDT
#From: fdeming@aecmail.Prime.COM (Frank Deming {x6088})
index($pair[1], $pair[0]) >= 0;
}
sub oldchildof {
local(@pair) = @_;
for (@pair) { s/^\d+\s+//g; }
index($pair[1], $pair[0]) >= 0;
}
__END__
--tom
--
Tom Christiansen tchrist@convex.com convex!tchrist
If you have ever seen the grim word "login:" on a screen, your mind
is now a wholly-owned subsidiary of The Death Star.
John Woods in <14105@ksr.com> of comp.unix.bsd
Technical Details
Message-ID:
<1992Aug3.033125.18468@news.eng.convex.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!cs.utexas.edu!convex!convex!tchristReferences:
<1992Jul31.035445.24776@latcs1.lat.oz.au>Show Full Headers
Complete Article Headers
Newsgroups: comp.unix.shell
Path: arc1-ca.pugleaf.net!sparky!uunet!cs.utexas.edu!convex!convex!tchrist
From: Tom Christiansen <tchrist@convex.COM>
Subject: Re: sort du output
Originator: tchrist@pixel.convex.com
Sender: usenet@news.eng.convex.com (news access account)
Message-ID: <1992Aug3.033125.18468@news.eng.convex.com>
Date: Mon, 3 Aug 1992 03:31:25 GMT
Reply-To: tchrist@convex.COM (Tom Christiansen)
References: <1992Jul31.035445.24776@latcs1.lat.oz.au>
Nntp-Posting-Host: pixel.convex.com
Organization: CONVEX Realtime Development, Colorado Springs, CO
X-Disclaimer: This message was written by a user at CONVEX Computer
Corp. The opinions expressed are those of the user and
not necessarily those of CONVEX.
Lines: 94