Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need Linux cmd-line app to compare binary files and exit on 1st mis-match

Is there a Linux command-line app which will compare two binary files and exit on the first mis-match?

cmp doesn't seem to have the quit opition.

like image 341
Peter.O Avatar asked Oct 25 '10 09:10

Peter.O


2 Answers

cmp doesn't have this option, because it always quits on the first mismatch.

$ cmp -b /bin/ls /bin/sed
/bin/ls /bin/sed differ: byte 25, line 1 is 320 M-P 300 M-@
like image 100
Fred Foo Avatar answered Oct 01 '22 01:10

Fred Foo


I think you could go by using 3 tools:

  • cmp
  • diff
  • md5sum

cmp is better for binary files and diff is better for text files For binary files diff merely reports whether they differ ot not. diff works also for directories.

Any of the first two could accomplish what you need silently. diff uses the -q switch and cmp uses the -s switch to tell you just a return code: 0 if the two files match 1 if not.

cmp has also a nice option to avoid (sort of) reading the entire file (good if you have big files): if you know that the files could differ in the first N lines or between line N and M you could do (i.e.: for row N = 10 and M = 20):

cmp file1 file2 10 20 

I added md5sum to the list because if you have the chance to calculate the MD5 checksum every time you edit one of those files, then you could compare only that to quickly find if they match or not. In this case I assume that you have a lot of file to compare.

like image 38
microspino Avatar answered Oct 01 '22 02:10

microspino