#!/usr/bin/perl
#-------------------------
# Find Duplcate files
#-------------------------
#
# load the modules, Load The Modules, LOAD THE MODULES
#
use File::Find; # used to traverse directory
use Digest::MD5; # use to find md5 of each file
#
# 1=print extra output, 0=more silent
#
my $debug=0;
#
# hash containing all data, md5 is the key, value array of file names.
#
my %unique=();
my $ref_unique=\%unique;
#
# If no arguments are given, use current dir
#
@ARGV = (".") unless @ARGV;
#
# Function calcuates md5 on files
# and populates the %unique hash using $ref_unique reference
#
sub md5sum {
#
# Unless a file, exit function
#
return unless -f ;
#
# Assign file name to variable
#
my $file="$File::Find::name";
#
# Create md5 object
#
my $md5 = Digest::MD5->new;
#
# Add file name to the object
#
$md5->add($file);
#
# Calculate md5sum, store in variable
#
my $number=$md5->clone->hexdigest;
#
# Print values if debug is turn;
#
if ( $debug == 1 ){ print "$number:$file\n"; }
#
# Now Add this info to the hash %unique
#
if ( exists $$ref_unique{$number} ){
#
# If the md5 has been seen before, append to hash elements array
#
push @{ $$ref_unique{"$number"} }, "$file" ;
} else {
#
# Otherwise Start a new hash element
# And add the file name to the array
#
my @array = ( $file );
$$ref_unique{$number} = [ ( $file ) ];
}
}
#
# Finally call find
#
find(\&md5sum, @ARGV);
#
# Print out the results
#
foreach my $x ( keys %unique ) {
my $y = $#{$unique{$x}} + 1;
print "$x:$y \t @{ $unique{$x} }\n"
}