Program: dutree

The dutree program, shown in Example 5.3, turns the output of du.

% du pcb

               19      pcb/fix
            
               20      pcb/rev/maybe/yes
            
               10      pcb/rev/maybe/not
            
               705     pcb/rev/maybe
            
               54      pcb/rev/web
            
               1371    pcb/rev
            
               3       pcb/pending/mine
            
               1016    pcb/pending
            
               2412    pcb

into sorted, indented output:

            
               2412 pcb
            
                  |    1371 rev
            
                  |       |    705 maybe
            
                  |       |      |      675 .
            
                  |       |      |       20 yes
            
                  |       |      |       10 not
            
                  |       |    612 .
            
                  |       |     54 web
            
                  |    1016 pending
            
                  |       |        1013 .
            
                  |       |           3 mine
            
                  |      19 fix
            
                  |       6 .

The arguments you give dutree are passed through to du. That way you could call dutree in any of these ways, or maybe more if your du supports other options.

% dutree
% dutree /usr
% dutree -a 
% dutree -a /bin

The %Dirsize hash maintains the mapping of names to sizes. For example, $Dirsize{"pcb"} contains 2412 in this sample run. We’ll use that hash both for output and for sorting each directory’s subdirectories by size.

%Kids is more interesting. For any given path $path, $Kids{$path} contains a (reference to an) array of names of subdirectories of this one. The "pcb" entry contains a reference to an anonymous array containing "fix", "rev", and "pending". The "rev" entry contains "maybe" and "web". The "maybe" entry contains "yes" and "not", which do not have their own entries because they are end nodes in the tree.

The output function is passed the start of the tree—the last line read in from the output of du. First it prints that directory and its size. Then the function sorts the directory’s children (if any) so that those with the most disk usage float to the ...

Get Perl Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.