I'm looking for a command that compares binary files.
Of course, I know about diff
, but it is not very good at binaries.
I have two files from a error-prone source (scratched dvd) which should be equal but aren't.
(Well, realy more than two, and I get about 6 different md5sum out of 15 samples.)
I'm looking for a tool that lists the positions where the files differ.
In addition to the accepted answer, xxd looks good as it can also be used to change back edited hex-files to binary.
1 Answer 1
To compare binary files and identify the exact positions where they differ, you can use cmp
, radiff2
, vimdiff
and more...
cmp
You can use cmp to compare two files. By default, a byte for byte binary comparison is done. If no differences are found, no output is written. If no option flags are specified, cmp writes a message with the byte and line number of the first difference and exits with an error.
cmp -l file1 file2
radiff2
radiff2 is a powerful tool within the radare2 suite designed for binary diffing. It can be somehow compared to the well known diff utility from UNIX, but with focus on comparing binary files.
It supports several types of diffing, including 1:1 binary diffing, delta diffing, code analysis diffing, and binary data (bindata) diffing.
radiff2 bin1 bin2
For diffing binaries with the intent of counting the differences, you might use
radiff2
, which you could search for in your Linux repository (might be found in the package radare2).
radiff2
has the parameter-c
to count binary differences.
vimdiff
What is vimdiff?
Vimdiff is a Linux command that can edit two, three, or four versions of a file with Vim and show their differences.
vimdiff file1 file2
Other Possible Methods and Tool Combinations for Information
We’ll talk about different ways to compare binary files in Linux. We may need this when investigating different files for data recovery, reverse engineering, and other programming problems.
colordiff
+ xxd
If you've
colordiff
, it can colorizediff
output, e.g.:
colordiff -y <(xxd foo1.bin) <(xxd foo2.bin)
Otherwise install via:
sudo apt-get install colordiff
Sample output:
binary file output in terminal - diff -y <(xxd foo1.bin) <(xxd foo2.bin) | colordiff
colordiff is a wrapper for diff and produces the same output as diff but with coloured syntax highlighting at the command line to improve readability.
od
with diff
hexdump
with diff
xxd
with diff
% xxd b1 > b1.hex % xxd b2 > b2.hex
And then
% diff b1.hex b2.hex
or
% vimdiff b1.hex b2.hex
cmp
with gawk
As we’ve seen, the results from the different solutions are equivalent and return similar results. Therefore, we should choose based on two main factors: if we have the required tools installed in our system or not and the desired formatting of the output.
They are all commands for dumping files and they can all dump it in various formats such as hexadecimal, octal or binary
binwalk
The firmware analysis tool
binwalk
also has this as a feature through its-W
/--hexdump
command line option which offers options such as to only show the differing bytes
Man pages
-
2I guess cmp is exactly what I asked for, now I need to evaluate if it is what I was looking for. First tries look promising.Gyro Gearloose– Gyro Gearloose2025年05月09日 19:48:36 +00:00Commented May 9 at 19:48
Explore related questions
See similar questions with these tags.
radiff2
, so test both if possible,cpm
andradiff2
and compare the results. Also, share your experience ^^