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

前端 未结 14 673
傲寒
傲寒 2020-12-07 09:25

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

相关标签:
14条回答
  • 2020-12-07 09:59

    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.

    0 讨论(0)
  • 2020-12-07 10:04

    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
    
    0 讨论(0)
  • 2020-12-07 10:05

    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 '  \|  '
    
    0 讨论(0)
  • 2020-12-07 10:07

    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

    0 讨论(0)
  • 2020-12-07 10:16

    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.

    0 讨论(0)
  • 2020-12-07 10:18

    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.

    0 讨论(0)
提交回复
热议问题