简体   繁体   中英

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. 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.

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 -n <bytes> file1 file2

From the man page: exit status is 0 if inputs are the same, 1 if different, 2 if trouble.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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