To demonstrate the work of the commands, we will create two binary files. Create file hello.c with the following contents:
#include <stdio.h> int main(void) { printf("Hello world!\n"); return(0); }
then compile it
# gcc hello.c -o h1.bin ./h1.bin
Hello world!
Now make change in hello.c add !
... printf("Hello world!!\n"); ...
recompile it and run
# gcc hello.c -o h2.bin ./h1.bin
Hello world!!
Now we have two binary files, one of them print on standart output "Hello world!" second "Hello world!!"
ls -l
total 44 -rwxr-xr-x. 1 root root 8512 Jul 2 13:30 h1.bin -rwxr-xr-x. 1 root root 8512 Jul 2 13:22 h2.bin -rw-r--r--. 1 root root 79 Jul 2 13:21 hello.c
As you can see, they have a same size, but we execly know that they are different.
cmp
The cmp
utility compares two files of any type byte by byte and writes the results to the standard output.
By default, cmp
is silent if the files are the same;
if they differ, the byte and line number at which the first difference occurred is reported.
# cmp h1.bin h2.bin
h1.bin h2.bin differ: byte 645, line 1
The cmp
command compares two files, and if they differ, tells the
first byte and line number where they differ or reports that one file is a prefix of the other.
Bytes and lines are numbered starting with 1.
Output of command can be more informable if you use some options:
# cmp -lb h1.bin h2.bin
645 265 M-5 225 M-^U 646 276 M-> 12 ^J 647 247 M-' 177 ^? 648 327 M-W 71 9 649 122 R 45 % 650 27 ^W 221 M-^Q 651 246 M-& 13 ^K 652 152 j 10 ^H 653 263 M-3 364 M-t 654 55 - 216 M-^N 655 1 ^A 52 * 656 231 M-^Y 375 M-} 657 136 ^ 213 M-^K 658 15 ^M 240 M- 660 271 M-9 334 M-\ 661 327 M-W 106 F 662 254 M-, 302 M-B 663 173 { 226 M-^V 664 36 ^^ 274 M-< 1517 0 ^@ 41 ! 7585 35 ^] 36 ^^
md5sum
Another simple way to compare two files is check MD5 (128-bit) checksums.
# md5sum h1.bin h2.bin
55bdf8972e7fb9464676c450a335d0a2 h1.bin 99f9015d8c4ac74d64cb7fd8cefd8c3f h2.bin
diff
With some options you can yse diff
command.
# diff -qs h1.bin h2.bin
Files h1.bin and h2.bin differ
# diff -qs h1.bin h1.bin
Files h1.bin and h1.bin are identical
No features.
No features.
No features.