Duplicates.pl

From OS2World.com Wiki
Jump to: navigation, search


Script

# duplicates.pl
#
# Dave Saville December 2003
# dave@deezee.org
#

use strict;
use warnings;
use File::Find ();

if ( ! @ARGV )
{
  print <<EOF;
Finds duplicate file names across disks
Just give it a list of disks to search
Suggest you point stdout to a file as it can be unexpectably large
eg perl duplicates.pl c d >duplicates.txt 
EOF
  exit;  
}

my %hash;

sub wanted;

while ( my $disk = shift )
{
  File::Find::find({wanted => \&wanted}, join '', substr($disk, 0, 1), ":/.");
}

foreach $_ (sort keys %hash)
{
  next if  @{$hash{$_}} == 1;
  print "$_\n";

  foreach my $thing (@{$hash{$_}})
  {
    print "\t$thing\n";
  }
}

exit;

sub wanted
{
  # build a hash of arrays of file locations keyed
  # by lower cased file names
  # 
  tr/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/; # lower case
  push @{$hash{$_}}, $File::Find::name;
}