Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sort du output

4,526 views
Skip to first unread message

/home/pgrds/wongm

unread,
Jul 30, 1992, 11:54:45 PM7/30/92
to
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 ?


Thanks in advance!

- wo...@latcs1.lat.oz.au

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- wo...@latcs1.lat.oz.au

Boyd Roberts

unread,
Jul 31, 1992, 7:03:39 PM7/31/92
to
du | sort -n +0 -1

RTFM


Boyd Roberts bo...@prl.dec.com

``When the going gets wierd, the weird turn pro...''

David W. Tamkin

unread,
Aug 1, 1992, 1:01:45 PM8/1/92
to
wo...@latcs1.lat.oz.au wrote in <1992Jul31.0...@latcs1.lat.oz.au>:

| 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.

du | sort -n or du | sort +0n

The -n option tells sort to compare by numerical magnitude. 10 precedes 2
lexically but 2 < 10 numerically.

David W. Tamkin Box 59297 Northtown Station, Illinois 60659-0297
dat...@ddsw1.mcs.com CompuServe: 73720,1570 MCI Mail: 426-1818

Tom Christiansen

unread,
Aug 2, 1992, 11:31:25 PM8/2/92
to
From the keyboard of wo...@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 -- tch...@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: fde...@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 tch...@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 <14...@ksr.com> of comp.unix.bsd

Patrick Lobo

unread,
Aug 3, 1992, 4:57:28 PM8/3/92
to
In article <1992Jul31.0...@latcs1.lat.oz.au> wo...@latcs1.lat.oz.au (/home/pgrds/wongm) writes:
>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 ?

A simple solution is to do something like this

du | sort -r +0 -1 | awk '{ printf("%-60s %s\n", $2, $1); }'

the sort -r is to reverse the sort according to file size. The -60s in the
printf statement assumes that pathnames will not exceed 60 characters length.
You need to modify this value of 60 to suit your purpose. The output is
formatted nicely with the above values though.

Patrick Lobo


Peer Landa

unread,
Aug 3, 1992, 8:45:12 PM8/3/92
to
Patrick Lobo writes


A even more simple solution is:
ls -l | sort -nr +3

-- peer

Patrick Lobo

unread,
Aug 4, 1992, 9:10:48 PM8/4/92
to
In article <1992Aug4.0...@leland.Stanford.EDU> pe...@ccrma.stanford.edu (Peer Landa) writes:
>Patrick Lobo writes

>> > du to bc or some similar command to do that ?
>>
>> A simple solution is to do something like this
>>
>> du | sort -r +0 -1 | awk '{ printf("%-60s %s\n", $2, $1); }'
>>
>> the sort -r is to reverse the sort according to file size. The -60s in the
>> printf statement assumes that pathnames will not exceed 60 characters length.
>> You need to modify this value of 60 to suit your purpose. The output is
>> formatted nicely with the above values though.
>>
>> Patrick Lobo
>
>
>A even more simple solution is:
>ls -l | sort -nr +3
>
>-- peer

This is not what the user asked for. He wants to sort the output of the du
command.

There are at least 2 reasons why ls won't do the job. First of all, du
travels down directories recursively giving a clean output that can be piped
to sort -nr +0 -1 as I have shown above. ls CAN travel directories
recursively using ls -lR, but its output cannot be simply piped to sort becaue
it contains extraneous stuff which has to be filtered out first.

Secondly, du gives the disk usage in blocks. ls -l gives the file size in
bytes which is not what the user asked for.

Patrick Lobo

0 new messages