venerdì 22 maggio 2015

Improvements to a little Perl script to find out duplicated files

I made a few changes to my little Perl script that finds duplicated files on a set of directories.
I always wanted to use File::Find::Rule, and this has been an appropriate context for plugging the module in so that the code now looks as follows:

die "\nPlease specify one or more directories\n" unless ( @ARGV );

my $files = {};
->exec( sub{ push @{ $files->{ digest_file( $_[2], "SHA-1" ) } }, $_[2]; } )
->in( @ARGV );

while ( my ($sha1, $files) = each %$files ){
say "\n\n#Duplicated files: \n\t#rm " . join( "\n\t#rm ", @$files ) if ( @$files >= 1 );

As you can see I've also removed a few tests for the arguments, since I rely on the in() method to check that every entry in @ARGV is a directory.

Nessun commento: