views:

42

answers:

2

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.

+3  A: 

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-@
larsmans
I'm pretty new to Linux, so I'll use that as an excuse :) ... I tried it with the -l option which ploughs on regardless.. I looked at (but didn't try) the -b option, becaues the man-page only said "Print differing bytes", with no mention of quitting... Live and learn! ... Thanks..
fred.bear
The `-b` option is just there for illustration, if omitted the behavior is still what you want. Sadly, many of the standard utilities (the ones from GNU) have very poor manpages.
larsmans
+1  A: 

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.

microspino
Thanks, but it wasn't so much the issue of *silent*, as it was about not chugging through 5 GB of data unnecessarily...
fred.bear
Again, thanks for the extra info.. I wanted this for a one-off compare of 2 DVD .iso's (and for future reference), so I was definitely after the quick-exit on mismatch... (Your extra effort in presenting a fuller answer gets you a mark from me :)
fred.bear