知道两个二进制文件是否相同(除了时间戳)最简单的方法是什么(在Ubuntu Linux上使用图形工具或命令行)?我不需要提取差值。我只需要知道它们是否相同。


当前回答

带有以下选项的Diff将进行二进制比较,以检查文件是否完全不同,如果文件相同,则输出:

diff -qs {file1} {file2}

如果你在比较两个不同目录下同名的文件,你可以使用这种形式:

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

OS X船长

其他回答

使用sha1生成校验和:

sha1 [FILENAME1]
sha1 [FILENAME2]

我最终使用hexdump将二进制文件转换为十六进制表示,然后在meld / kompare /任何其他diff工具中打开它们。我不像你,我要找的是文件中的差异。

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

meld tmp/hex1.txt tmp/hex2.txt
md5sum binary1 binary2

如果md5sum相同,则二进制文件相同

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

试试不同的

简单的回答:使用-s开关运行diff。

长话短说:继续往下读。


举个例子。让我们先创建两个包含随机二进制内容的文件:

$ 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

现在让我们复制第一个文件:

$ cp test1.bin copyoftest1.bin

现在test1.bin和test2.bin应该是不同的:

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

... test1.bin和copyoftest1.bin应该是相同的:

$ diff test1.bin copyoftest1.bin

但是等等!为什么没有输出?!?

答案是:这是有意为之。在相同的文件上没有输出。

但是有不同的错误代码:

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

$ echo $?
1


$ diff test1.bin copyoftest1.bin

$ echo $?
0

幸运的是,现在你不必每次都检查错误代码,因为你可以使用-s(或——report- identity -files)开关来让diff变得更详细:

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

使用cmp命令。有关更多信息,请参阅二进制文件和强制文本比较。

cmp -b file1 file2