bash 如何使用“cmp”比较两个二进制文件并找到它们不同的所有字节偏移量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8385618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:17:20  来源:igfitidea点击:

How to use "cmp" to compare two binaries and find all the byte offsets where they differ?

bashshellunix

提问by lewis denny

I would love some help with a Bash script loop that will show all the differences between two binary files, using just

我希望得到一些有关 Bash 脚本循环的帮助,该循环将显示两个二进制文件之间的所有差异,仅使用

cmp file1 file2 

It only shows the first change I would like to use cmp because it gives a offset an a line number of where each change is but if you think there's a better command I'm open to it :) thanks

它只显示我想使用 cmp 的第一个更改,因为它提供了一个偏移量和每个更改所在位置的行号,但如果您认为有更好的命令,我愿意接受它:) 谢谢

回答by rwos

I think cmp -l file1 file2might do what you want. From the manpage:

我想cmp -l file1 file2可能会做你想做的。从联机帮助页:

-l  --verbose
      Output byte numbers and values of all differing bytes.

The output is a table of the offset, the byte value in file1 and the value in file2 for all differing bytes. It looks like this:

输出是偏移量表,file1 中的字节值和 file2 中所有不同字节的值。它看起来像这样:

4531  66  63
4532  63  65
4533  64  67
4580  72  40
4581  40  55
[...]

So the first difference is at offset 4531, where file1's decimal byte value is 66 and file2's is 63.

所以第一个区别是偏移量 4531,其中文件 1 的十进制字节值为 66,文件 2 的值为 63。

回答by mouviciel

The more efficient workaround I've found is to translate binary files to some form of text using od.

我发现的更有效的解决方法是使用od.

Then any flavour of diffworks fine.

然后任何味道都diff很好。