size per day of a directory

Discussion in 'Perl Misc' started by George Bouras, Feb 14, 2013.

  1. George Bouras, Feb 14, 2013
    #1
    1. Advertisements

  2. here it is



    #!/usr/bin/perl
    # Show the data size per date
    # data_per_day.pl /data/cache

    use strict;
    use warnings;
    use File::Find;

    my $dir = exists $ARGV[0] ? ( -d $ARGV[0] ? $ARGV[0] : die "Not existing
    directory: $ARGV[0]\n" ) : ( die "Specify an existing directory as
    argument\n" );
    my %Info = ();
    my @Month = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;

    File::Find::find({wanted=>sub {
    return unless -f $File::Find::name;
    my $size = -s _;
    my @Time = localtime( (stat _)[9] ); $Time[5]+=1900;
    my $key = sprintf "%04d%02d%02d", @Time[5,4,3];
    $Info{$key}->{'total files'}++ ;
    $Info{$key}->{'total size'} += $size;
    },no_chdir=>1, bydepth=>0, follow=>0}, $dir);

    foreach my $date (sort {$a <=> $b} keys %Info) {
    my ($y,$m,$d) = $date =~/^(\d{4})(\d\d)(\d\d)/;
    print "$d $Month[$m] $y\n";
    print "\ttotal files : $Info{$date}->{'total files'}\n";
    print "\ttotal size : ", __Human_size( $Info{$date}->{'total size'} )
    ,"\n"
    }

    sub __Human_size {
    if ( $_[0] < 1024 ) { return "$_[0] Bytes" }
    elsif ( $_[0] < 1024**2 ) { return sprintf "%.1f Kb" , ($_[0]/(1024**1)) }
    elsif ( $_[0] < 1024**3 ) { return sprintf "%.1f Mb" , ($_[0]/(1024**2)) }
    elsif ( $_[0] < 1024**4 ) { return sprintf "%.2f Gb" , ($_[0]/(1024**3)) }
    elsif ( $_[0] < 1024**5 ) { return sprintf "%.2f Tb" , ($_[0]/(1024**4)) }
    elsif ( $_[0] < 1024**6 ) { return sprintf "%.2f Pb" , ($_[0]/(1024**5)) }
    elsif ( $_[0] < 1024**7 ) { return sprintf "%.2f Eb" , ($_[0]/(1024**6)) }
    else { return $_[0] }
    }
     
    George Bouras, Feb 14, 2013
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.