Subversion Repositories sysadmin_scripts

Rev

Rev 162 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
158 rodolico 1
#! /usr/bin/env perl
2
 
162 rodolico 3
# fdupesGreatestSavings
4
#
5
# Filter which takes the output of fdupes --size and reports on which duplicated
6
# files clean up will result in the greatest savings.
7
# 
8
# Usage: fdupes --size --recurse / | fdupesGreatestSavings 100
9
#
10
# Copyright 2024 Daily Data, Inc.
11
# 
12
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following 
13
# conditions are met:
14
#
15
#   Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
16
#   Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer 
17
#   in the documentation and/or other materials provided with the distribution.
18
#   Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
19
#   from this software without specific prior written permission.
20
# 
21
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
22
# NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 
158 rodolico 28
use strict;
29
use warnings;
30
use Data::Dumper;
31
 
32
# takes a path and returns an array of the path for each subdirectory
33
sub parseDirectory {
34
   use File::Basename;
35
   my $separator = '/';
36
   my @result;
37
   my @temp = split( $separator, dirname( shift ) );
38
   my $curpath = '';
39
   for ( my $i = 0; $i < scalar( @temp ); $i++ ) {
40
      $curpath .= ($curpath ? $separator : '' ) . $temp[$i];
41
      push @result, $curpath;
42
   }
43
   return \@result;
44
}
45
 
46
my $maxCount = shift;
47
die "Enter maximum entries to show\n" unless $maxCount;
48
 
49
my $entry = 0; # just a simple index into our data
50
my %data; # hash for all our data
51
my $files = (); # temporary array for our file list
52
while ( my $line = <> ) {
53
   chomp $line;
54
   if ( $line =~ m/^(\d+) bytes each:$/ ) { # new entry
55
      $data{++$entry}{'size'} = $1;
56
      $data{$entry}{'files'} = [];
57
   } elsif ( $line =~ m/^\s*$/ ) { # blank line, so get summary
58
      $data{$entry}{'total'} = $data{$entry}{'size'} * scalar( @{$data{$entry}{'files'}} );
59
   } else { # this should be a file name
60
      push @{ $data{$entry}{'files'} }, $line;
61
   }
62
}
63
 
64
#print Dumper( \%data ) . "\n"; die;
65
 
66
foreach my $thisEntry ( sort{ $data{$b}{'total'} <=> $data{$a}{'total'} } keys %data ) {
67
   last unless $maxCount--;
163 rodolico 68
   my $numCopies = scalar( @{$data{$thisEntry}{'files'}} );
158 rodolico 69
   print "$data{$thisEntry}{total} bytes wasted in following " . $numCopies . " files of $data{$thisEntry}{size} bytes\n";
70
   print "\t" . join( "\n\t", @{$data{$thisEntry}{'files'}} ) . "\n\n";
71
}
72
 
73
exit 1;
74
 
75
my %directories;
76
 
77
foreach my $thisEntry ( keys %data ) {
78
   foreach my $file ( @{$data{$thisEntry}{'files'}} ) {
79
      my $parsedDir = &parseDirectory( $file );
80
      foreach my $thisDir( @$parsedDir ) {
81
         $directories{$thisDir}{'size'} += $data{$thisEntry}{'size'} * 1;
82
         push @{ $directories{$thisDir}{'files'} }, $file;
83
      }
84
   }
85
}
86
 
87
foreach my $thisEntry ( sort{ $data{$b}{'size'} <=> $data{$a}{'size'} } keys %directories ) {
88
   last unless $maxCount--;
89
   print "$directories{$thisEntry}{size}\t$thisEntry\n\t";
90
   print join( "\n\t", @{ $directories{$thisEntry}{'files'} } ) . "\n\n";
91
}
92
 
93
 
94
1;