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.
|
|---|