Search code examples
linuxdiffbinaryfiles

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.


Solution

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