Linux cmp command: compare two files by bytes
Linux cmp command Function Description
Use the cmp command to compare two files by bytes and see if the files differ.
Linux cmp command Syntax
cmp [option] [file1] [file2 [Byte count1 [Byte count2]]]
The meaning of each option in the command is shown in the table:
Option | Description |
---|---|
-l | Output the number of bytes and values of all different bytes |
-n <Byte count> |
Compare by specified minimum bytes |
-b | Display different bytes |
-i <Byte count> |
Skip the first few bytes of the loser |
-i <Byte Count1>:<Byte Count2> |
Skip the first few bytes of file 1 and file 2 |
-s | No error message is displayed |
Linux cmp command Demo
Compare apidemos.com.sort.txt and apidemos.com.txt files by bytes
cmp apidemos.com.sort.txt apidemos.com.txt
Output:
Compare /root/ab and /root/ac files by bytes
[root@rhel ~]# cmp /root/ab /root/ac
/root/ab /root/ac differ: byte 6, line 2
Compare /root/ab and /root/ac files by byte, skipping the first 2 bytes of input
[root@rhel ~]# cmp -i 2 /root/ab /root/ac
Compare /root/ab and /root/ac files by byte, skipping the first 1 byte of file 1 and the first 2 bytes of file 2
[root@rhel ~]# cmp -i 1:2 /root/ab /root/ac
Compare /root/ab and /root/ac files by byte, output byte count and value.
[root@rhel ~]# cmp -l /root/ab /root/ac
6 12144
cmp: EOF on /root/ab