#!/usr/bin/perl # ################################################################### # # Name: avgsizer.pl # Author: pete@rasterweb.net # Date Created: 06.06.2002 # Date Modified: 06.06.2002 # # Purpose: Someone asked me to write a script to get the total # and average file size of a directory (and subdirectories) # # 10 minutes later I had this... # # This is free software, you may use it and distribute it under the # same terms as Perl itself. # #################################################################### #################################################################### # use use File::Find; # edit as needed... my $thedir = '/path/to/directory'; my @dirs = ($thedir); my $total = 0; find(\&sizefiles, @dirs); foreach $itemsize (@sizes) { $total = $total + $itemsize unless ($itemsize < 1); } $howmany = @sizes; $average = sprintf "%.3f k", (($total / 1024) / $howmany); $totalk = sprintf "%.3f k", ($total / 1024); print "\n\n"; print "Files found: $howmany\n"; print "Total size of all files: $totalk\n"; print "Average file size: $average\n"; print "\n"; ################### # subs below here # #################################################################### #################################################################### sub sizefiles { my $file = $File::Find::name; my $dir = $File::Find::dir; if (!(-d ($file))) { my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($file); print "$file: $size\n"; push (@sizes, $size); } } # end sub sizefiles __END__