sawa
sawa

Reputation: 168269

How to compare binary files to check if they are the same?

What is the easiest way (using a graphical tool or command line on Ubuntu Linux) to know if two binary files are the same or not (except for the time stamps)? I do not need to actually extract the difference. I just need to know whether they are the same or not.

Upvotes: 292

Views: 367557

Answers (15)

Francewhoa
Francewhoa

Reputation: 81

wxHexEditor

wxHexEditor is both free and able to Diff large files up to 2^64 bytes (2 ExaByte). Has a GUI. Cross-platform. Lots of features.

To get it for free, choose one of the following options:


Below is the same suggestion as above. But with details if you're interested in those.

Screenshot

enter image description here

Strength

• Hexadecimal (Hex) Editor. Which is helpful for doing reverse Engineering.

• Cross-platform. Linux, Mac OS, Windows

• Easy to use Graphical User Interface (GUI)

• Supports very large files up to 2^64 bytes (2 ExaByte)

• Compare two large files side by side (diff). Optionally list and search all diff.

• Very fast search

• Use small amount of RAM

• Do not create temporary files. So it used a very small amount of storage space.

• Dark or bright theme

• Multilingual 15 languages

• Open source. If you are not familiar with "open source", it means this software has both stronger security & stronger privacy. Because its code is publicly available for review and contributions to GitHub at https://github.com/EUA/wxHexEditor or at SourceForge at https://sourceforge.net/p/wxhexeditor/code/

• Attractive GNU General Public License version 2. This means the software code of this extension is owned and supported by a friendly not-for-profit community. Instead of a for-profit corporation. https://github.com/EUA/wxHexEditor/blob/master/LICENSE

Challenge

• Confusion between the two code repositories. At the time of this writing, August 2021, the GitHub repository seems to be more recent. As it was last updated in 2021 at https://github.com/EUA/wxHexEditor In comparison, the SourceForge repository at https://sourceforge.net/projects/wxhexeditor/ was last update of wxHexEditor was December 31st, 2017.

Show Your Support

• If you enjoy this application, show your support to the authors & contributors with:

___• Donation at https://www.paypal.com/cgi-bin/webscr?item_name=Donation+to+wxHexEditor&cmd=_donations&business=erdem.ua%40gmail.com

___• Support with ticket at https://sourceforge.net/projects/wxhexeditor/support

___• Support with forum at https://sourceforge.net/p/wxhexeditor/discussion/

___• Patch at https://sourceforge.net/p/wxhexeditor/patches/

Using

• wxHexEditor 0.23

• Debian 10 Buster

• GNOME 3.30.2

Upvotes: 4

shao.lo
shao.lo

Reputation: 4636

I found Visual Binary Diff was what I was looking for, available on:

  • Ubuntu:

    sudo apt install vbindiff
    
  • Arch Linux:

    sudo pacman -S vbindiff
    
  • Mac OS X via MacPorts:

    port install vbindiff
    
  • Mac OS X via Homebrew:

    brew install vbindiff
    

Upvotes: 153

Rikki
Rikki

Reputation: 1239

You can use MD5 hash function to check if two files are the same, with this you can not see the differences in a low level, but is a quick way to compare two files.

md5 <filename1>
md5 <filename2>

If both MD5 hashes (the command output) are the same, then, the two files are not different.

Upvotes: 25

Michal Ambroz
Michal Ambroz

Reputation: 139

My favourite ones using xxd hex-dumper from the vim package :

1) using vimdiff (part of vim)

#!/bin/bash
FILE1="$1"
FILE2="$2"
vimdiff <( xxd "$FILE1" ) <( xxd "$FILE2" )

2) using diff

#!/bin/bash
FILE1=$1
FILE2=$2
diff -W 140 -y <( xxd $FILE1 ) <( xxd $FILE2 ) | colordiff | less -R -p '  \|  '

Upvotes: 2

funnydman
funnydman

Reputation: 11386

Radiff2 is a tool designed to compare binary files, similar to how regular diff compares text files.

Try radiff2 which is a part of radare2 disassembler. For instance, with this command:

radiff2 -x file1.bin file2.bin

You get pretty formatted two columns output where differences are highlighted.

Upvotes: 2

ashish
ashish

Reputation: 377

md5sum binary1 binary2

If the md5sum is same, binaries are same

E.g

md5sum new*
89c60189c3fa7ab5c96ae121ec43bd4a  new.txt
89c60189c3fa7ab5c96ae121ec43bd4a  new1.txt
root@TinyDistro:~# cat new*
aa55 aa55 0000 8010 7738
aa55 aa55 0000 8010 7738


root@TinyDistro:~# cat new*
aa55 aa55 000 8010 7738
aa55 aa55 0000 8010 7738
root@TinyDistro:~# md5sum new*
4a7f86919d4ac00c6206e11fca462c6f  new.txt
89c60189c3fa7ab5c96ae121ec43bd4a  new1.txt

Upvotes: 3

Ahab Devoid
Ahab Devoid

Reputation: 47

There is a relatively simple way to check if two binary files are the same.

If you use file input/output in a programming language; you can store each bit of both the binary files into their own arrays.

At this point the check is as simple as :

if(file1 != file2){
    //do this
}else{
    /do that
}

Upvotes: -4

StackzOfZtuff
StackzOfZtuff

Reputation: 3108

Try diff -s

Short answer: run diff with the -s switch.

Long answer: read on below.


Here's an example. Let's start by creating two files with random binary contents:

$ dd if=/dev/random bs=1k count=1 of=test1.bin
1+0 records in
1+0 records out
1024 bytes (1,0 kB, 1,0 KiB) copied, 0,0100332 s, 102 kB/s

                                                                                  
$ dd if=/dev/random bs=1k count=1 of=test2.bin
1+0 records in
1+0 records out
1024 bytes (1,0 kB, 1,0 KiB) copied, 0,0102889 s, 99,5 kB/s

Now let's make a copy of the first file:

$ cp test1.bin copyoftest1.bin

Now test1.bin and test2.bin should be different:

$ diff test1.bin test2.bin
Binary files test1.bin and test2.bin differ

... and test1.bin and copyoftest1.bin should be identical:

$ diff test1.bin copyoftest1.bin

But wait! Why is there no output?!?

The answer is: this is by design. There is no output on identical files.

But there are different error codes:

$ diff test1.bin test2.bin
Binary files test1.bin and test2.bin differ

$ echo $?
1


$ diff test1.bin copyoftest1.bin

$ echo $?
0

Now fortunately you don't have to check error codes each and every time because you can just use the -s (or --report-identical-files) switch to make diff be more verbose:

$ diff -s test1.bin copyoftest1.bin
Files test1.bin and copyoftest1.bin are identical

Upvotes: 9

Joe
Joe

Reputation: 42666

The standard unix diff will show if the files are the same or not:

[me@host ~]$ diff 1.bin 2.bin
Binary files 1.bin and 2.bin differ

If there is no output from the command, it means that the files have no differences.

Upvotes: 293

Dima Korobskiy
Dima Korobskiy

Reputation: 1556

Diff with the following options would do a binary comparison to check just if the files are different at all and it'd output if the files are the same as well:

diff -qs {file1} {file2}

If you are comparing two files with the same name in different directories, you can use this form instead:

diff -qs {file1} --to-file={dir2}

OS X El Capitan

Upvotes: 6

simotek
simotek

Reputation: 745

I ended up using hexdump to convert the binary files to there hex representation and then opened them in meld / kompare / any other diff tool. Unlike you I was after the differences in the files.

hexdump tmp/Circle_24.png > tmp/hex1.txt
hexdump /tmp/Circle_24.png > tmp/hex2.txt

meld tmp/hex1.txt tmp/hex2.txt

Upvotes: 21

bobjandal
bobjandal

Reputation: 2373

Use cmp command. This will either exit cleanly if they are binary equal, or it will print out where the first difference occurs and exit.

Upvotes: 145

Daniel Alder
Daniel Alder

Reputation: 5382

For finding flash memory defects, I had to write this script which shows all 1K blocks which contain differences (not only the first one as cmp -b does)

#!/bin/sh

f1=testinput.dat
f2=testoutput.dat

size=$(stat -c%s $f1)
i=0
while [ $i -lt $size ]; do
  if ! r="`cmp -n 1024 -i $i -b $f1 $f2`"; then
    printf "%8x: %s\n" $i "$r"
  fi
  i=$(expr $i + 1024)
done

Output:

   2d400: testinput.dat testoutput.dat differ: byte 3, line 1 is 200 M-^@ 240 M- 
   2dc00: testinput.dat testoutput.dat differ: byte 8, line 1 is 327 M-W 127 W
   4d000: testinput.dat testoutput.dat differ: byte 37, line 1 is 270 M-8 260 M-0
   4d400: testinput.dat testoutput.dat differ: byte 19, line 1 is  46 &  44 $

Disclaimer: I hacked the script in 5 min. It doesn't support command line arguments nor does it support spaces in file names

Upvotes: 4

user2008151314
user2008151314

Reputation: 680

Use cmp command. Refer to Binary Files and Forcing Text Comparisons for more information.

cmp -b file1 file2

Upvotes: 6

Scott Presnell
Scott Presnell

Reputation: 1538

Use sha1 to generate checksum:

sha1 [FILENAME1]
sha1 [FILENAME2]

Upvotes: 19

Related Questions