#!/usr/local/bin/perl # A Perl CGI script to graphicly show the directories # and their sizes. Doug Steinwand # start the search in this directory: $TREEROOT="/home/dzs/html"; use File::Find; # determine the size of each directory, # (without counting symlinks) find( sub { if (-f && !-l) { $dirsize{$File::Find::dir}+=-s; $filecount{$File::Find::dir}++; } }, $TREEROOT); # sort the list of directory names @dirs=sort keys %dirsize; if ($ENV{'QUERY_STRING'}=~/size/i) { # sort by size @dirs=sort {$dirsize{$b} <=> $dirsize{$a}} @dirs; } elsif ($ENV{'QUERY_STRING'}=~/file/i) { # sort by number of files @dirs=sort {$filecount{$b} <=> $filecount{$a}} @dirs; } # calculate total bytes $totalsize=0; foreach (values %dirsize) { $totalsize+=$_; } # eliminate any chance of dividing by zero: $totalsize=1 if $totalsize==0; # calculate total files $totalfiles=0; foreach (values %filecount) { $totalfiles+=$_; } print < Size of directories in $TREEROOT
Directory of $TREEROOT

EOT foreach (@dirs) { # format size in percent $p=sprintf("%1.0f",100*$dirsize{$_}/$totalsize); $n=100-$p; # format size in kilobytes $k=korm($dirsize{$_}); # remove html root directory from the results $name=$_; $name=~s/^$TREEROOT//; $name="/" unless length($name); # print the directory name print < EOT } $t=korm($totalsize); print <
Directory Files Size Percent
$name $filecount{$_} $k
  |
$p%

Totals:

$totalfiles

$t

100%
EOT exit; # returns kilobytes or megabytes sub korm { my($n)=shift; my($r)=sprintf("%1.1fk",$n/1024); if (length $r > 6) { $r=sprintf("%1.1fm",$n/1048576); } return $r; }