#!/usr/bin/perl -w # # Script: filestats # # Description: Print stats on files in a tree, inc. average size, # distribution and counts for file types # (Note: undoubtably not the most elegant solution) # # Author: Ade Rixon, ar@rsi.co.uk # # History: v0.2, 24/9/98 # use File::Basename; use File::Find; $idxcnt = 0; $bytecount = 0; usage() if (@ARGV == 0); find(\&wanted, @ARGV) or usage(); # process the dirs # output stats $mean = int($bytecount/$filecount); print "Average file size is: $bytecount/$filecount = ", $mean, " bytes\n"; print "\nDistribution of file sizes:\n"; print "Size (K)\tNo. of files\n"; for ($i=0; $i <= scalar(@filesizes); $i++) { if ($filesizes[$i]) { printf "%8d - %d\t%12d\n", $i*10, $i*10+9, $filesizes[$i]; } } print "\nFile report by type (extension):\n"; print "Type\tNo. of files\tSize (bytes)\n"; $extsize = 0; foreach $ext (sort keys %extidx) { printf "%-8s%8d\t%s\n", $ext, $extfiles[$extidx{$ext}], $extbytes[$extidx{$ext}]; $extcnt += $extfiles[$extidx{$ext}]; $extsize += $extbytes[$extidx{$ext}]; } printf "\nTotals\t%8d\t%s\n", $extcnt, $extsize; exit; sub usage { die("Usage: $0 \n"); } # subroutine for find sub wanted { my $file = $_; return 0 if (! -f $file); #($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size) = stat($file); $size = -s $file; # update total bytes, no. of files and array of sizes in K $bytecount += $size; $filecount++; $filesizes[int($size/10240)]++; # get filename extension $ext = (fileparse($file, '\.\w\w?\w?\w?$'))[2]; $ext = 'other' if ($ext eq ""); # default $ext = lc($ext); # ignore case # %extidx gives indexes into array for each file ext # @extfiles, @extbytes contain file and byte count for each ext if (!defined($extidx{$ext})) { # initialise new array element $extidx{$ext} = $idxcnt++; # index $extbytes[$extidx{$ext}] = 0; # byte count } $extbytes[$extidx{$ext}] += $size; $extfiles[$extidx{$ext}]++; }