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
I think
cmp -l file1 file2
might do what you want. From the manpage: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:
So the first difference is at offset 4531, where file1's decimal byte value is 66 and file2's is 63.
Method that works for byte addition / deletion
Generate a test case with a single removal of byte 64:
Output:
If you also want to see the ASCII version of the character:
Output:
Tested on Ubuntu 16.04.
I prefer
od
overxxd
because:xxd
is not (comes with Vim)-An
to remove the address column withoutawk
.Command explanation:
-An
removes the address column. This is important otherwise all lines would differ after a byte addition / removal.-w1
puts one byte per line, so that diff can consume it. It is crucial to have one byte per line, or else every line after a deletion would become out of phase and differ. Unfortunately, this is not POSIX, but present in GNU.-tx1
is the representation you want, change to any possible value, as long as you keep 1 byte per line.-v
prevents asterisk repetition abbreviation*
which might interfere with the diffpaste -d '' - -
joins every two lines. We need it because the hex and ASCII go into separate adjacent lines. Taken from: Concatenating every other line with the next()
to definebdiff
instead of{}
to limit the scope of the inner functionf
, see also: How to define a function inside another function in bashSee also:
The more efficient workaround I've found is to translate binary files to some form of text using
od
.Then any flavour of
diff
works fine.