karthik.raju has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I want to compare 2 folders recursively including files and contents,

This script satisfies 80% of my requirement, but i want to print the matching files also, and i tried a lot but cant able to print the similar files.

How i can print the similar files too.
Please help...
use File::DirCompare; use File::Basename; sub compare_dirs{ my ($dir1, $dir2) = @_; my $equal = 1; File::DirCompare->compare($dir1, $dir2, sub { my ($a,$b) = @_; $equal = 0; # if the callback was called even once, the dirs are n +ot equal if ( !$b ) { printf "File '%s' only exists in dir '%s'.\n", basename($a), dir +name($a); } elsif ( !$a ) { printf "File '%s' only exists in dir '%s'.\n", basename($b), dir +name($b); } else { printf "File contents for $a and $b are different.\n"; } }); return $equal; } print "Please specify two directory names\n" and exit if (@ARGV < 2); printf "%s\n", &compare_dirs($ARGV[0], $ARGV[1]) ? 'Test: PASSED' : 'T +est: FAILED';
  • Comment on Script to compare 2 folders, including sub folders and files with content
  • Download Code

Replies are listed 'Best First'.
Re: Script to compare 2 folders, including sub folders and files with content
by haukex (Archbishop) on Jul 02, 2016 at 11:49 UTC

    Hi karthik.raju,

    Have a look at the "matches" option in File::DirCompare. For example, adding this option to your script works for me: matches => sub { print "File contents $_[0] and $_[1] are identical.\n" }

    By the way, as the File::DirCompare documentation mentions, your script is essentially doing the same thing as diff: diff --recursive --brief --report-identical-files (or diff -rqs)

    Hope this helps,
    -- Hauke D

      Hi Hauke, Thanks for your reply and i've tried with your answer

       matches => sub { print "File contents $_[0] and $_[1] are identical.\n" }
      but since i'm new to Subroutines so cant able to fit this in right place.

      since i'm getting error like "Not a subroutine: 'matches'".
      Can please give the correct place that where and how i need to call this.

        Hi karthik.raju,

        I suggest you re-read the documentation of File::DirCompare closely, as it gives examples of the correct syntax with which to supply options: they are supplied as a hash reference ({ matches => ... }) as the last argument to the compare function. If you still have trouble please see How do I post a question effectively? and show the exact error messages and the code needed to reproduce them.

        Regards,
        -- Hauke D

Re: Script to compare 2 folders, including sub folders and files with content
by afoken (Chancellor) on Jul 02, 2016 at 11:58 UTC

    diff -Nur /path/to/dir1 /path/to/dir2 should do the trick, list all differences in text files, and name different binary files. If all files are text files, but diff fails to recognise some of them, force ASCII mode: diff -Naur /path/to/dir1 /path/to/dir2.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      -qrs would be better since OP is only listing files, not the actual diff:

      $ diff -qrs /tmp/A /tmp/B Only in /tmp/A: Math.aux Files /tmp/A/Math.log and /tmp/B/Math.log differ Files /tmp/A/Math.pdf and /tmp/B/Math.pdf differ Files /tmp/A/Math.tex and /tmp/B/Math.tex differ Files /tmp/A/MathLogo.jpg and /tmp/B/MathLogo.jpg are identical

      Update: Oh oops, looks like haukex already said this... I missed it behind the part of their post that actually answered the original question :). Think of this post as highlighting the alternate solution.

      Good Day,
          Dean