22 1 月 2013

用Perl找出目錄下檔案大小分佈

作者:Ben哥|發布日期:2010/11/15

這個程式用來找出某個目錄下,檔案大小的分布狀況,可以用於微調NFS及儲存設備的掛載參數及設定

#!/usr/bin/perl

die "\nUsage: $0 (directory)\nExactly 1 argument must be provided and must be a
directory.\n" unless -d $ARGV[ 0 ];

use strict;
use File::Find;
use Cwd;

 

my %file_size = ();

find( \&find_size, "$ARGV[ 0 ]" );

sub find_size { my $cwd = cwd();
if ( -f $_ )
{
my $size = -s "$cwd/$_" ;

$file_size{ 'total files'}++;

if ( $size <= 4096 )
{
$file_size{ '04k' }++;
}
elsif ( $size <= 8192 )
{
$file_size{ '08k' }++;
}
elsif ( $size<= 16384 )
{
$file_size{ '16k' }++;
}
elsif ( $size<= 32768 )
{
$file_size{ '32k' }++;
}
else
{
$file_size{ 'more than 32k' }++;
}
}
}

foreach ( sort { $a cmp $b } keys %file_size )
{
print "$_ : $file_size{ $_ }\n";
}


To emulate the real world, I use a kernel tree as the program argument. The kernel has been compiled so there are many object files. Here are the program running sample and result:

[benlai@testbed ~]$ ./find_size.pl

Usage: ./find_size.pl <directory>
Exactly 1 argument must be provided and must be a directory.
[benlai@server ~]$ vi find_size.pl
[benlai@server ~]$ time ./find_size.pl embedded/src/kernels/linux-2.6.32/
Total number of files are : 31767
04k : 15713
08k : 5346
16k : 4771
32k : 3404
more than 32k : 2533
total files : 31767

real 1m51.838s
user 0m10.953s
sys 1m21.101s
[benlai@testbed ~]$