Cómo comparar archivos binarios para comprobar si son los mismos?


¿Cuál es la forma más fácil (usando una herramienta gráfica o línea de comandos en Ubuntu Linux) de saber si dos archivos binarios son iguales o no (excepto por las marcas de tiempo)? No necesito realmente extraer la diferencia. Sólo necesito saber si son iguales o no.

10 answers

El unix estándar diff mostrará si los archivos son los mismos o no:

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

Si no hay salida del comando, significa que los archivos no tienen diferencias.

 116
Author: Joe,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-12 13:09:34

Use el comando cmp. Esto saldrá limpiamente si son binarios iguales, o imprimirá donde ocurre la primera diferencia y saldrá.

 76
Author: bobjandal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-05 20:44:31

Encontré Visual Binary Diff era lo que estaba buscando, disponible en:

  • Ubuntu: apt-get install vbindiff
  • Mac OS X: port install vbindiff vía MacPorts .
  • Mac OS X: brew install vbindiff a través de Homebrew
 61
Author: shao.lo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-04 20:09:55

Use sha1 para generar suma de verificación:

sha1 [FILENAME1]
sha1 [FILENAME2]
 14
Author: Scott Presnell,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-08-25 01:21:06

Terminé usando hexdump para convertir los archivos binarios a una representación hexadecimal y luego los abrí en meld / kompare / cualquier otra herramienta de diferencias. A diferencia de ti, yo buscaba las diferencias en los archivos.

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

meld tmp/hex1.txt tmp/hex2.txt
 9
Author: simotek,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-10 03:46:19

Utilice el comando cmp. Consulte Archivos binarios y Forzando Comparaciones de texto para obtener más información.

cmp -b file1 file2
 6
Author: user2008151314,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-12-08 23:50:30

md5 <filename1>

md5 <filename2>

Ver si son los mismos: -)

 4
Author: Rikki,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-27 15:19:49

Diff con las siguientes opciones haría una comparación binaria para comprobar solo si los archivos son diferentes en absoluto y saldría si los archivos son los mismos también:

diff -qs {file1} {file2}

Si está comparando dos archivos con el mismo nombre en directorios diferentes, puede usar este formulario en su lugar:

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

OS X El Capitan

 4
Author: DKroot,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-11 18:04:37

Para encontrar defectos de memoria flash, tuve que escribir este script que muestra todos los bloques 1K que contienen diferencias (no solo el primero como cmp -b lo hace)

#!/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

Salida:

   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 $

Descargo de responsabilidad: Hackeé el script en 5 min. No admite argumentos de línea de comandos ni espacios en nombres de archivo

 3
Author: Daniel Alder,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-03-09 14:05:54

Prueba diff-s

Respuesta corta: ejecuta diff con el interruptor -s.

Respuesta larga: sigue leyendo abajo.


Aquí hay un ejemplo. Comencemos por crear dos archivos con contenido binario aleatorio:

$ 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

Ahora vamos a hacer una copia del primer archivo:

$ cp test1.bin copyoftest1.bin

Ahora prueba 1.bin y test2.bin debe ser diferente:

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

... y test1.bin y copyoftest1.bin debe ser idéntico:

$ diff test1.bin copyoftest1.bin

Pero espera! ¿Por qué no hay de salida?!?

La respuesta es: "esto es por diseño. No hay salida en archivos idénticos.

Pero hay diferentes códigos de error:

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

$ echo $?
1


$ diff test1.bin copyoftest1.bin

$ echo $?
0

Ahora, afortunadamente, no tiene que verificar los códigos de error cada vez porque puede usar el -s (o --report-identical-files) cambia para que diff sea más detallado:

$ diff -s test1.bin copyoftest1.bin
Files test1.bin and copyoftest1.bin are identical
 3
Author: StackzOfZtuff,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-05 12:02:56