#!/usr/bin/perl # Find and list duplicted files from a given path # Coded by Trizen on 1st January 2012 # http://trizen.googlecode.com use strict; use File::Find; use File::Compare; my %files; @ARGV ? find( sub { push @{$files{-s _}}, $File::Find::name if -f; }, grep{chr ord ne '-'} @ARGV ) : die "usage: $0 /my/path\n"; my @dups; my $index = 0; my $keep_last = grep{/^-+keep[-_]last$/} @ARGV; my $keep_first = grep{/^-+keep[-_]first$/} @ARGV; foreach my $size (keys %files) { next if $#{$files{$size}} < 1; my $previous_file = shift @{$files{$size}}; foreach my $cur_file (@{$files{$size}}) { unless (compare($cur_file, $previous_file)) { push @{$dups[$index]}, ($cur_file, $previous_file); } $previous_file = $cur_file; } ++$index; } { local $, = $/; local $\ = $/ x 2; foreach my $i (0 .. $#dups) { next unless defined $dups[$i]; my %table; @table{@{$dups[$i]}} = (); my @files = sort { lc $a cmp lc $b } keys %table; print @files; if($keep_first){ for my $i(1..$#files){ print "** Deleting $files[$i]"; unlink $files[$i] or warn "Unable to delete: $!\n"; } } elsif($keep_last){ for my $i(0..$#files-1){ print "** Deleting $files[$i]"; unlink $files[$i] or warn "Unable to delete: $!\n"; } } } }