Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

shell

unix

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

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

like image 722
Lewis Denny Avatar asked Dec 05 '11 12:12

Lewis Denny


People also ask

How do I compare two binary files?

Use the command cmp to check if two files are the same byte by byte. The command cmp does not list differences like the diff command. However it is handy for a fast check of whether two files are the same or not (especially useful for binary data files).

Does diff work on binary files?

You can also force diff to consider all files to be binary files, and report only whether they differ (but not how). Use the `--brief' option for this. In operating systems that distinguish between text and binary files, diff normally reads and writes all data as text.

How do I compare two binary files in Ubuntu?

If you want to compare two files byte by byte, you can use the cmp program with the --verbose ( -l ) option to show the values of each differing byte in the two files. With GNU cmp , you can also use the -b or --print-bytes option to show the ASCII representation of those bytes. See Invoking cmp , for more information.


1 Answers

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

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

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 octal byte value is 66 and file2's is 63.

like image 68
rwos Avatar answered Oct 13 '22 21:10

rwos