Subversion Repositories sysadmin_scripts

Rev

Rev 162 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
158 rodolico 1
#! /usr/bin/env perl
2
 
3
use strict;
4
use warnings;
5
use Data::Dumper;
6
 
7
# takes a path and returns an array of the path for each subdirectory
8
sub parseDirectory {
9
   use File::Basename;
10
   my $separator = '/';
11
   my @result;
12
   my @temp = split( $separator, dirname( shift ) );
13
   my $curpath = '';
14
   for ( my $i = 0; $i < scalar( @temp ); $i++ ) {
15
      $curpath .= ($curpath ? $separator : '' ) . $temp[$i];
16
      push @result, $curpath;
17
   }
18
   return \@result;
19
}
20
 
21
my $maxCount = shift;
22
die "Enter maximum entries to show\n" unless $maxCount;
23
 
24
my $entry = 0; # just a simple index into our data
25
my %data; # hash for all our data
26
my $files = (); # temporary array for our file list
27
while ( my $line = <> ) {
28
   chomp $line;
29
   if ( $line =~ m/^(\d+) bytes each:$/ ) { # new entry
30
      $data{++$entry}{'size'} = $1;
31
      $data{$entry}{'files'} = [];
32
   } elsif ( $line =~ m/^\s*$/ ) { # blank line, so get summary
33
      $data{$entry}{'total'} = $data{$entry}{'size'} * scalar( @{$data{$entry}{'files'}} );
34
   } else { # this should be a file name
35
      push @{ $data{$entry}{'files'} }, $line;
36
   }
37
}
38
 
39
#print Dumper( \%data ) . "\n"; die;
40
 
41
foreach my $thisEntry ( sort{ $data{$b}{'total'} <=> $data{$a}{'total'} } keys %data ) {
42
   last unless $maxCount--;
43
   my $numCopies = scalar( @{$data{$entry}{'files'}} );
44
   print "$data{$thisEntry}{total} bytes wasted in following " . $numCopies . " files of $data{$thisEntry}{size} bytes\n";
45
   print "\t" . join( "\n\t", @{$data{$thisEntry}{'files'}} ) . "\n\n";
46
}
47
 
48
exit 1;
49
 
50
my %directories;
51
 
52
foreach my $thisEntry ( keys %data ) {
53
   foreach my $file ( @{$data{$thisEntry}{'files'}} ) {
54
      my $parsedDir = &parseDirectory( $file );
55
      foreach my $thisDir( @$parsedDir ) {
56
         $directories{$thisDir}{'size'} += $data{$thisEntry}{'size'} * 1;
57
         push @{ $directories{$thisDir}{'files'} }, $file;
58
      }
59
   }
60
}
61
 
62
foreach my $thisEntry ( sort{ $data{$b}{'size'} <=> $data{$a}{'size'} } keys %directories ) {
63
   last unless $maxCount--;
64
   print "$directories{$thisEntry}{size}\t$thisEntry\n\t";
65
   print join( "\n\t", @{ $directories{$thisEntry}{'files'} } ) . "\n\n";
66
}
67
 
68
 
69
1;