简体   繁体   English

如何比较 Linux 中二进制文件的前 N 个字节

[英]How to compare first N bytes of binary files in Linux

I have two binary files with different sizes.我有两个不同大小的二进制文件。 I need to compare first N bytes of these files in Linux.我需要比较 Linux 中这些文件的前 N 个字节。 I expect that the result is either "yes" (the same) or "no" (not the same), not byte-to-byte comparing.我希望结果是“是”(相同)或“否”(不相同),而不是逐字节比较。 The N may vary from KBs to GBs. N 可能从 KB 到 GB 不同。

Currently I'm using the following approach:目前我正在使用以下方法:

head -c N input1.dat | rdiff signature >1.sig
head -c N input2.dat | rdiff signature >2.sig
diff 1.sig 2.sig

But I'm wondering if there is another approach, more simple.但我想知道是否有另一种更简单的方法。 Thanks.谢谢。

Try cmp :尝试cmp

cmp -n <bytes> file1 file2

From the man page: exit status is 0 if inputs are the same, 1 if different, 2 if trouble.从手册页中:如果输入相同,则退出状态为 0,如果不同,则为 1,如果出现故障,则为 2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM