Takes 2 parms: dir_init and show_level
#!/usr/bin/perl
use strict;
my $debug = 0;
my ($dir_init, $show_level) = @ARGV;
my (@ls, @dirs, $size, $parent);
my %dir_size = ();
my %parent_of_dir = ();
my %level_of_dir = ();
my $sep = "~" x 80;
my $form = "%20s %s\n";
my $files = 0;
# Get the list of files using readdir.
push(@dirs, $dir_init);
$dir_size{$dir_init} = 0;
$level_of_dir{$dir_init} = 0;
foreach my $directory (@dirs) {
print "\n\n------- starting directory scan -------- in $directory"
+ if ($debug);
opendir (DIR, $directory) or die ("Can't open dir: '$directory'.\n
+Reason: $!");
@ls = readdir(DIR);
closedir (DIR);
# Then go through the results of ls and work out the files..
FILE: foreach my $file (@ls)
{
# print "\n* * * * * $file" if ($debug);
next if ($file =~ m/^\.\.?$/);
$files++;
SWITCH:
{
if (-d "$directory/$file") {
print "\n$file\tis DIRECTORY" if ($debug);
push(@dirs, "$directory/$file");
$level_of_dir{"$directory/$file"} = $level_of_dir{$dir
+ectory} + 1;
$parent_of_dir{"$directory/$file"} = $directory;
$dir_size{"$directory/$file"} = 0;
last SWITCH;
}
if (-l "$directory/$file") {
print "\n$file is a symbolic link" if ($debug);
last SWITCH;
}
if (-f "$directory/$file") {
print "\n$file is a plain file" if ($debug);
# ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,
+$mtime,$ctime,$blksize,$blocks)= stat "$directory/$file";
$size = (-s "$directory/$file");
print "\tsize: $size" if ($debug);
$dir_size{"$directory"} += $size;
$parent = $parent_of_dir{$directory};
# print "\ntry with parent as: $parent if ($debug);
while ($parent)
{
$dir_size{$parent} += $size;
$parent = $parent_of_dir{$parent};
# print "\ntry with parent as: $parent" if ($debug)
+;
}
last SWITCH;
}
else
{
print "\n$file\tis of UNKNOWN type";
}
}
}
}
print "\n------- finished directory scans --------" if ($debug);
print "Total number of files/directories examined: $files\n";
print $sep . "\n";
foreach my $k (sort keys %dir_size)
{
next if ($show_level && ($show_level < $level_of_dir{$k}));
print "\t\t" x $level_of_dir{$k};
printf $form, commify($dir_size{$k}), $k;
# printf $form, $dir_size{$k}, $k;
}
print $sep . "\n";
print "\n" if ($debug);
foreach my $k (keys %parent_of_dir)
{
print "\n $k: $parent_of_dir{$k}" if ($debug);
}
sub commify {
my $text = reverse $_[0];
$text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
return scalar reverse $text;
}
__OUTPUT__
Total number of files/directories examined: 1489
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+~~~~~~~~~~
58,192,935 P:\dev\perl
145,431 P:\dev\perl/0735712891Code
1,129,217 P:\dev\perl/Compress-Zlib-1.33
34,215,542 P:\dev\perl/DPL
507,956 P:\dev\perl/Regexp-Common-2.113
71,412 P:\dev\perl/albums
508,041 P:\dev\perl/calendar
65,996 P:\dev\perl/formalware
53,356 P:\dev\perl/guestbook
1,714 P:\dev\perl/hoch
12,139 P:\dev\perl/http
11,692,406 P:\dev\perl/modules
100,426 P:\dev\perl/oracle
31,036 P:\dev\perl/perl-xml-quickstart
758,171 P:\dev\perl/updf
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+~~~~~~~~~~
|