-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdutree.pl
59 lines (50 loc) · 1.29 KB
/
dutree.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/perl -w
# dutree - print sorted indented rendition of du output
use strict;
my %Dirsize;
my %Kids;
getdots(my $topdir = input());
output($topdir);
sub input {
my ($size, $name, $parent);
@ARGV = ("du @ARGV |");
while (<>) {
($size, $name) = split;
$Dirsize{$name} = $size;
($parent = $name) =~ s#/[^/]+$##; # dirname
push @{$Kids{$parent}}, $name unless eof;
}
return $name;
}
sub getdots {
my $root = $_[0];
my ($size, $cursize);
$size = $cursize = $Dirsize{$root};
if ($Kids{$root}) {
for my $kid (@{ $Kids{$root}}) {
$cursize -= $Dirsize{$kid};
getdots($kid);
}
}
}
sub output {
my ($root, $prefix, $width) = (shift, shift || '', shift || 0);
my $path;
($path = $root) =~ s#.*/##;
my $size = $Dirsize{$root};
my $line = sprintf("%${width}d %s", $size, $path);
print $prefix, $line, "\n";
for ($prefix .= $line) {
s/\d /| /;
s/[^|]/ /g;
}
if ($Kids{$root}) {
my @Kids = @{ $Kids{$root} };
@Kids = sort { $Dirsize{$b} <=> $Dirsize{$a} } @Kids;
$Dirsize{$Kids[0]} =~ /(\d+)/;
my $width = length $1;
for my $kid (@Kids) {
output($kid, $prefix, $width);
}
}
}