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.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Recursive Directory Comparison and Export Differences by thanos1983

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.