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

别说谁变了你拦得住时间么 提交于 2019-12-13 13:12:51

问题


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.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/4013223/need-linux-cmd-line-app-to-compare-binary-files-and-exit-on-1st-mis-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!