#!/usr/bin/perl my $gDebug = 0; my %fileTypeCounts; my %fileTypeSizes; # Sizeof(filename) returns (#blocks,#bytes,#files,#dirs) for the item, and its # contents if a directory. sub Sizeof { my $filename = shift; my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($filename); my $isDir = ($mode & 0x00004000) != 0; my $filesCount; my $dirsCount; if($isDir) { $filesCount = 0; $dirsCount = 1; } else { $filesCount = 1; $dirsCount = 0; my $fileType = "(none)"; if($filename =~ /^.*(\.[^.\/]+?)$/) { $fileType = $1; } $fileTypeCounts{$fileType} = 0 if(!$fileTypeCounts{$fileType}); $fileTypeSizes{$fileType} = 0 if(!$fileTypeSizes{$fileType}); $fileTypeCounts{$fileType}++; $fileTypeSizes{$fileType} += $size; } print "$filename: $blocks blocks, $size bytes\n" if $gDebug; printf "mode: %08x\n",$mode if $gDebug; if($isDir) { my $subSize,$subBlocks,$subFile; my @subFiles; my $subFilesCount,$subDirsCount; my $openOK; $openOK = opendir(DIR,$filename); if(!$openOK) { print " couldn't open $filename\n"; } else { @subFiles = readdir(DIR); closedir(DIR); foreach $subFile (@subFiles) { if($subFile ne "." && $subFile ne "..") { ($subBlocks,$subSize,$subFilesCount,$subDirsCount) = Sizeof($filename . "/" . $subFile); $blocks += $subBlocks; $size += $subSize; $filesCount += $subFilesCount; $dirsCount += $subDirsCount; } } } } return ($blocks,$size,$filesCount,$dirsCount); } my $arg = shift; my ($blocks,$size,$filesCount,$dirsCount) = Sizeof($arg); my $sizeComma = ""; my $sizex = $size; while($sizex != 0) { if($sizeComma ne "") { $sizeComma = "," . $sizeComma; } my $a = $sizex % 1000; my $b = int($sizex / 1000); my $fmt = $b > 0 ? "%03d" : "%d"; $sizeComma = sprintf($fmt,$a) . $sizeComma; $sizex = $b; } foreach my $type (sort(keys(%fileTypeCounts))) { printf "%30s: %3d files %10d bytes\n",$type,$fileTypeCounts{$type},$fileTypeSizes{$type}; } print "$arg -- blocks: $blocks, bytes: $size ($sizeComma), "; print "files: $filesCount, directories: $dirsCount\n";