When I wrote this script, I didn't think to look for a module that did the same thing. However, I am pretty happy with the way this one turned out.

This script will look through a directory and return a list of all the files with the same filename and byte size with all of the files with the same content noted. A few user modifications will be needed to set an optional base directory or exclude certain files.

#!/usr/bin/perl use strict; use warnings; use File::Compare; use File::Find; #If you want to set a base_directory, you can do so here. my $base_directory; print "What directory? "; my $directory = <>; chomp $directory; my @file_list; sub files_wanted { my $text = $File::Find::name; if ( -f ) { push @file_list, $text; } } #If you set a base directory above, you will need to change $directory to $base_directory.$directory. find(\&files_wanted,$directory); #This section creates a hash of arrays of files, with the hash keys be +ing filename.ext and the file size in #parentheses. The raw file name is the entire path including the file +name. my %files; for my $raw_file (@file_list) { my @file_parts = split(/\//,$raw_file); my $file = pop @file_parts; my $file_size = -s $raw_file; push @{$files{"$file ($file_size bytes)"}}, $raw_file; } #This section searches the hash for any file with 2 or more files whic +h share the same filename.ext and size. #After that, it compares all of the files with those attributes to det +ermine if they share the same contents. #It will print the list of files with the same filename and size and w +ill tell you which ones share the same #contents. for my $file (sort keys %files) { if (@{$files{$file}} > 1) { my $amount = @{$files{$file}}; print "$file\t\t$amount\n"; for my $location1 (@{$files{$file}}) { print "\t$location1\n"; for my $location2 (@{$files{$file}}) { unless ($location1 eq $location2) { if (compare($location1,$location2) == 0) { print "\t\tExact copy: $location2\n"; } } } } print "\n"; } }

Update: This is my 100th write-up.

Have a cookie and a very nice day!
Lady Aleena

In reply to Find duplicate files with exact same files noted by Lady_Aleena

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.