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

前端 未结 14 682
傲寒
傲寒 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: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

提交回复
热议问题