试试 diff -s
简答:使用-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 (or --report-identical-files) switch 使 diff 更加详细:
$ diff -s test1.bin copyoftest1.bin
Files test1.bin and copyoftest1.bin are identical