DLR
DLR

Reputation: 187

Compare two directories for differences in regular files

Need to compare two directories and check through every file in both directories for files of the same name, if the same name occurs you check to see if the files have the same content, if so print > file <. If the file has the same name but not the same content < file > If there is a file that's not in directory 2 but in directory 1 <<< file1, and likewise >>> file2 for a file in dir 2 but not dir 1. I have been having trouble, my code doesn't even compare when I test to see if the files are equal in name.

#!/usr/bin/perl -w 
use File::Basename;

@files1 = `/usr/bin/find $ARGV[0] -print`;
chop @files1;
@files2 = `/usr/bin/find $ARGV[1] -print`;
chop @files2;

here: 
for ($i=1; @files1 >= $i; $i++) {

    for ($x=1; @files2 >= $x; $x++) {

        $file1 = basename($files1[$i]); 
        $file2 = basename($files2[$x]); 

        if ($file1 eq $file2) {

            shift @files1;
            shift @files2;

            $result = `/usr/bin/diff -q $files1[$i] $files2[$x]`;
            chop $result;

            if ($result eq "Files $files1[$i] and $files2[$x] differ") { 

                print "< $file1 >\n";
                next here;
            } 
            else { 

                print "> $file1 <\n";
            }
        }
        else  { 

            if ( !-e "$files1[$i]/$file2") { print ">>> $file2\n";}
            unless ( -e "$files2[$x]/$file1") { print "<<< $file1\n";}
        }
    }
}

Upvotes: 2

Views: 1699

Answers (1)

Gilles Qu&#233;not
Gilles Qu&#233;not

Reputation: 185254

Try using this :

diff -aqr /dir1 /dir2

or :

#!/bin/bash

for f;
    for g; do
        [[ "$f" != "$g" ]] &&
            cmp &>/dev/null "$f" "$g" || echo "$f is different of $g"
    done
done

USE this

./script dir1/* dir2/*

Upvotes: 3

Related Questions