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

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


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-@


I think you could go by using 3 tools:

  • cmp
  • diff
  • md5sum

cmp is better for binary files and diff is better for text filesFor 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.