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. Advertising

  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. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S01aX3N0YXRl?=

    Quick one - Is SESSION per browser instance or per IP Address?

    =?Utf-8?B?S01aX3N0YXRl?=, Apr 4, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    5,883
    gerry
    Apr 10, 2006
  2. Razvan
    Replies:
    1
    Views:
    414
    tony vee
    Sep 10, 2004
  3. Replies:
    5
    Views:
    2,545
  4. Replies:
    0
    Views:
    346
  5. Andy
    Replies:
    1
    Views:
    703
Loading...

Share This Page