I am trying to find the difference between two directories in Perl. I want to optimize it to run efficiently and also not sure how to ignore certain files (say with extension .txt or .o)
The code I have so far is:
use strict;
use warnings;
use Parallel::ForkManager;
use File::Find;
use List::MoreUtils qw(uniq);
my $dir1 = "/path/to/dir/first";
my $dir2 = "/path/to/dir/second";
my @comps = ('abc');
my (%files1, %files2);
my $workernum = 500;
my $pm = new Parallel::ForkManager($workernum);
my @common = ();
my @differ = ();
my @only_in_first = ();
my @only_in_second = ();
foreach my $comp (@comps) {
find( sub { -f ($files1{$_} = $File::Find::name) }, "$dir1");
find( sub { -f ($files2{$_} = $File::Find::name) }, "$dir2");
my @all = uniq(keys %files1, keys %files2);
for my $file (@all) {
my $pid = $pm->start and next; # do the fork
my $result;
if ($files1{$file} && $files2{$file}) { # file exists in both dirs
$result = qx(/usr/bin/diff -q $files1{$file} $files2{$file});
if ($result =~m/^Common subdirectories/) {
push (@common, $result);
} else {
push (@differ, $result);
}
} elsif ($files1{$file}) {
push (@only_in_first, $file);
} else {
push (@only_in_second, $file);
}
$pm->finish; # do the exit in child process
}
}