Subversion Repositories sysadmin_scripts

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#! /usr/bin/env perl

use strict;
use warnings;
use Data::Dumper;

# takes a path and returns an array of the path for each subdirectory
sub parseDirectory {
   use File::Basename;
   my $separator = '/';
   my @result;
   my @temp = split( $separator, dirname( shift ) );
   my $curpath = '';
   for ( my $i = 0; $i < scalar( @temp ); $i++ ) {
      $curpath .= ($curpath ? $separator : '' ) . $temp[$i];
      push @result, $curpath;
   }
   return \@result;
}

my $maxCount = shift;
die "Enter maximum entries to show\n" unless $maxCount;

my $entry = 0; # just a simple index into our data
my %data; # hash for all our data
my $files = (); # temporary array for our file list
while ( my $line = <> ) {
   chomp $line;
   if ( $line =~ m/^(\d+) bytes each:$/ ) { # new entry
      $data{++$entry}{'size'} = $1;
      $data{$entry}{'files'} = [];
   } elsif ( $line =~ m/^\s*$/ ) { # blank line, so get summary
      $data{$entry}{'total'} = $data{$entry}{'size'} * scalar( @{$data{$entry}{'files'}} );
   } else { # this should be a file name
      push @{ $data{$entry}{'files'} }, $line;
   }
}

#print Dumper( \%data ) . "\n"; die;

foreach my $thisEntry ( sort{ $data{$b}{'total'} <=> $data{$a}{'total'} } keys %data ) {
   last unless $maxCount--;
   my $numCopies = scalar( @{$data{$entry}{'files'}} );
   print "$data{$thisEntry}{total} bytes wasted in following " . $numCopies . " files of $data{$thisEntry}{size} bytes\n";
   print "\t" . join( "\n\t", @{$data{$thisEntry}{'files'}} ) . "\n\n";
}

exit 1;

my %directories;

foreach my $thisEntry ( keys %data ) {
   foreach my $file ( @{$data{$thisEntry}{'files'}} ) {
      my $parsedDir = &parseDirectory( $file );
      foreach my $thisDir( @$parsedDir ) {
         $directories{$thisDir}{'size'} += $data{$thisEntry}{'size'} * 1;
         push @{ $directories{$thisDir}{'files'} }, $file;
      }
   }
}
  
foreach my $thisEntry ( sort{ $data{$b}{'size'} <=> $data{$a}{'size'} } keys %directories ) {
   last unless $maxCount--;
   print "$directories{$thisEntry}{size}\t$thisEntry\n\t";
   print join( "\n\t", @{ $directories{$thisEntry}{'files'} } ) . "\n\n";
}

 
1;