Compare the checksum of files in 2 different folders

Hi

I have 2 different folders on different machines. they are supposed to be same but some time for unknown reason they are not. then we have to generate a report for files which are not matching.

I was doing as below -

cd folder1
find . -type f | sort | cksum >1.txt

cd folder2
find . -type f | sort | cksum >2.txt

1)

diff -w 1.txt 2.txt

still it shows lot of lines in diff even when file name/path/size/checksum is same. is there any nice and clean way to report the expected result.

2) i was able to generate good results (but not good format) by using -

cat 1.txt 2.txt | sort -k 3,3 | uniq -u

it does not tell me which file is from which folder (there is no < or > sign as we have in diff)

is there any other best way to do it ?

Thanks
Rel

You may want to list, sort and find cksum of each file as shown in the following example:

$ # go to directory dir1
$ cd dir1
$
$ ls | sort | xargs cksum
1958180199 14 file1
1994293486 14 file2
1996893289 14 file3
$
$ # now do the same for directory dir2
$ cd ../dir2
$
$ ls | sort | xargs cksum
1958180199 14 file1
1994293486 14 file2
1996893289 14 file3
$
$

And as you are doing currently, redirect the output to two files and find the diff.

tyler_durden

tyler_durden
thanks for suggestion
there are 2 issues

1) first ls wont show the files in sub directories. so we have to use find command. thats fine

2) main issue is still the same. if you do the diff of output from folder 1 and output from folder 2 it wont show properly as both the folder will have different no files so sort will be differnet and so the diff

diff directory1 directory2

if you can use Python, you can use its filecmp module

#!/usr/bin/env python
import filecmp
import os
directory1 = os.path.join("./","dir1")
directory2 = os.path.join("./","dir2")
cmp = filecmp.dircmp(directory1,directory2)
print cmp.report()
# print cmp.common
# print cmp.same_files # diff_files etc....

sample output

$ ls -1 dir1 dir2
dir1:
1.txt
2.txt
3.txt

dir2:
1.txt
3.txt
4.txt

$ ./python.py
diff ./dir1 ./dir2
Only in ./dir1 : ['2.txt']
Only in ./dir2 : ['4.txt']
Identical files : ['1.txt', '3.txt']
None

How about using chksums -c option? On the first machine run

cd /dir1
find . -type f -exec chksum {} \; > chksums

Copy it over to the other machine and run

cd /dir2
chksum -c chksums

That should give you a listing of those files that differ.

Or if you would like to program in Perl, then you may want to consider using the File:: Dircmp module -

$
$ cat -n dc.pl
     1  #!/usr/bin/perl -w
     2  use File::Dircmp;
     3  @r = dircmp("dir1","dir2");
     4  foreach $i (@r) {
     5    print $i,"\n";
     6  }
$
$ perl dc.pl
Only in dir1: dir11
Only in dir2: dir21
Files dir1/file2 and dir2/file2 are identical
Files dir1/file1 and dir2/file1 are identical
Files dir1/file3 and dir2/file3 differ
$
$

tyler_durden

Thanks Guys
but pls consider this also - I have 2 different folders on different machines and i need to verify checksum of each file

---------- Post updated at 01:01 PM ---------- Previous update was at 12:52 PM ----------

Thanks Guys
but pls consider this also - I have 2 different folders on different machines and i need to verify checksum of each file