Recently I learned I had duplicate copies of pictures from my camera on my system. Approximately 990 of them. Rather than compare two pictures side by side, I decided to compute the SHA1 (160 bit) checksums of the 'duplicate' pictures and compare that the SHA1 checksums of the 'master' files.

I'm sure my code could use some polishing. Comments welcome.

Update - Thanks to all for the comments, suggestions to make it work more efficiently. When I wrote this, it was written as a one time shot. The duplicate files got there in the first place through some wizardry of BOFH.

#!/usr/bin/perl -w use strict; use File::Find; my $dupedir ='/Big-Drive/NIKON-Pictures/Duplicates/'; my $count; my $calccount; my @files; my @calcsum; my $compared; open(OUT,">results.txt") || die "Can't open file $!"; find(\&files, $dupedir); &calcsum; # &printsums; &comparefiles; print "$count total files found\n"; print "$calccount total files calculated SHA1 sums\n"; print "$compared total files compared to originals\n"; sub files { return unless -f $File::Find::name; $count++; push (@files, "$File::Find::name"); } sub calcsum{ foreach(@files){ print "Computing sha1sum for $_\n"; push(@calcsum, `sha1sum $_`); $calccount++; } } sub printsums{ foreach(@calcsum){ print; } } sub comparefiles{ my $sum; my $file; my $calcsum; my $rest; foreach (@calcsum){ ($sum, $file)=split /\s+/; $file =~ s/Duplicates\///; if ( -f $file){ print "Calculating SHA1 checksum for file $file"; print OUT "Calculating SHA1 checksum for file $file"; ($calcsum, $rest)=split /\s+/,`sha1sum $file`; if ( $calcsum eq $sum){ print " ----> OK !\n"; print OUT " ----> OK !\n"; $compared++; }else{ print "\n****** ERROR ****** Checksums do not match for $file\ +n "; print OUT "\n****** ERROR ****** Checksums do not match for $f +ile\n "; } }else{ print "$file not in master directory ... skipping\n"; print OUT "$file not in master directory ... skipping\n"; } } } close OUT;

In reply to Comparing duplicate pictures in different directories by cajun

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.