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

前端 未结 14 672
傲寒
傲寒 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:52
    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
    
    0 讨论(0)
  • 2020-12-07 09:52

    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
    }
    
    0 讨论(0)
  • 2020-12-07 09:56

    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

    0 讨论(0)
  • 2020-12-07 09:57

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

    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.

    0 讨论(0)
  • 2020-12-07 09:58

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

    cmp -b file1 file2
    
    0 讨论(0)
提交回复
热议问题