thanos1983 has asked for the wisdom of the Perl Monks concerning the following question:
Hello again Monks,
I was trying to create a script that will recursively check for differences between directories and print them.
In UNIX the most common is diff -r test1/ test2/.
Sample of output:
$ diff -r test1/ test2/ diff -r test1/common/common.txt test2/common/common.txt 1c1 < This is a common line --- > This is a common line. Only in test2/common: common.txt~ Only in test1/: different1 Only in test2/: different2
I came across with multiple questions and suggestions for example (Directory comparison) and (Recursive Directory Comparison Tools).
I also found the File::DirCompare module. Sample of script and output:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; sub check_dirs { die "Please provide 2 Directories!\n" unless ($#ARGV == 1); for (@_) { die "Input $_ is not a directory!\n" unless (-d $_) }; } check_dirs(@ARGV); use File::DirCompare; # Simple diff -r --brief replacement use File::Basename; File::DirCompare->compare($ARGV[0], $ARGV[1], sub { my ($a, $b) = @_; if (! $b) { printf "Only in %s: %s\n", dirname($a), basename($a); } elsif (! $a) { printf "Only in %s: %s\n", dirname($b), basename($b); } else { print "Files $a and $b differ\n"; } }); __END__ $ perl cmp.pl test1/ test2/ Files test1/common/common.txt and test2/common/common.txt differ Only in test2/common: common.txt~ Only in test1: different1 Only in test2: different2
But it does tell provides you with the information of which file(s) are different.So I decided to write my own script.
Script and output:
#!/usr/bin/perl use strict; use warnings; use Text::Diff; use File::Dircmp; use Data::Dumper; sub check_dirs { die "Please provide 2 Directories!\n" unless ($#ARGV == 1); for (@_) { die "Input $_ is not a directory!\n" unless (-d $_) }; } check_dirs(@ARGV); my @dircmp = dircmp($ARGV[0], $ARGV[1]); my @diff; foreach my $element (@dircmp) { if ($element =~ /^Files/) { my @tmp = split / /, $element; push @diff, diff $tmp[1] => $tmp[3]; } } print Dumper \@dircmp if (@dircmp); print @diff if (@diff); __END__ $ perl cmp.pl test1/ test2/ $VAR1 = [ 'Only in test1/: different1', 'Only in test2/: different2', 'Only in test2//common: common.txt~', 'Files test1//common/common.txt and test2//common/common.txt + differ' ]; --- test1//common/common.txt Wed Mar 8 11:41:08 2017 +++ test2//common/common.txt Wed Mar 8 11:44:35 2017 @@ -1 +1 @@ -This is a common line +This is a common line.
So my question is, have a missed something? Anyone knows any other module that I could use? I am not really happy the way my output is printed is there anything that I could do to make it better?
Thanks in advance for our time and effort reading and replying to my question.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Recursive Directory Comparison and Export Differences
by Corion (Patriarch) on Mar 08, 2017 at 12:54 UTC | |
|
Re: Recursive Directory Comparison and Export Differences
by shmem (Chancellor) on Mar 09, 2017 at 11:52 UTC | |
|
Re: Recursive Directory Comparison and Export Differences
by Anonymous Monk on Mar 08, 2017 at 17:24 UTC |