5 UNIX / Linux cmp Command Examples

What is cmp?
5 cmp examples
Syntax and Options
Related Commands

What is cmp?

cmp command is used to compare two files byte by byte.

5 cmp Examples

Two provide examples for this command, lets consider two files :

file1.txt

$ cat file1.txt
Hi My name is Himanshu

file2.txt

$ cat file2.txt
Hi My name is Himanshu Arora

1. Directly compare using cmp

If we straight away try to compare the two files mentioned above, then we see :

 $ cmp file1.txt file2.txt 
file1.txt file2.txt differ: byte 23, line 1

So we see that cmp utility instantly recognized the difference between contents in two files.

2. Skip same number of initial bytes from both input files

If we want to skip some initial bytes of input files and then start the comparison then -i option can be used

$ cmp -i 5 file1.txt file2.txt 
file1.txt file2.txt differ: byte 18, line 1

So we see that the initial 5 bytes were skipped and hence cmp complains that byte 18 differs (which in example 1 was byte 23)

3. Skip different number of initial bytes from both input files

Another case could be when we would want to skip ‘x’ bytes of input in file1 and ‘y’ bytes of input in file2. This could be achieved as follows :

$ cmp -i 5:2 file1.txt file2.txt 
file1.txt file2.txt differ: byte 2, line 1

So we see that we can give the option -i with ‘x:y’ kind of input for this case. The output shows that this time the difference occurred very early at the second byte.

4. Display bytes that differ

If in the example 3 above we would also like to know the values of individual differing bytes being compared then the -l option can be used

$ cmp -l -i 5:2 file1.txt file2.txt 
 2 156 115
 3 141 171
 4 155  40
 5 145 156
 6  40 141
 7 151 155
 8 163 145
10 110 151
11 151 163
12 155  40
13 141 110
14 156 151
15 163 155
16 150 141
17 165 156
18  12 163
cmp: EOF on file1.txt

So we see can easily observe the difference in byte2 above.

5. Upper limit on number of bytes to compare

Also, we can put a limit on number of bytes to compare. This can be used when we want to compare only a specified number of bytes and not the complete file. This can be achieved through -n option which is followed by the number of bytes.
So in the above example 4 if we want to put an upper limit on the bytes to be compared, it can be done as follows :

$ cmp -l -n 10 -i 5:2 file1.txt file2.txt 
 2 156 115
 3 141 171
 4 155  40
 5 145 156
 6  40 141
 7 151 155
 8 163 145
10 110 151

Syntax and Options

cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]
Short Option Long Option Option Description
-b –print-bytes Print differing bytes.
-i SKIP –ignore-initial=SKIP Skip the first SKIP bytes of input
-i SKIP1:SKIP2 –ignore-initial=SKIP1:SKIP2 Skip the first SKIP1 bytes of FILE1 and the first SKIP2 bytes of FILE2.
–no-preserve-root do not treat `/’ specially (the default)
-l –verbose Output byte numbers and values of all differing bytes.
-n LIMIT –bytes=LIMIT Compare at most LIMIT bytes.
-s –quiet –silent Output nothing; yield exit status only.
-v –version Output version info.
–help Output this help.

Related Commands

Comments on this entry are closed.

  • charly July 5, 2012, 7:10 pm

    $ cmp file1.txt file2.txt
    file1.txt file2.txt differ: byte 23, line 1

    Can any one explain what is the meaning for byte:23, line 1 in output

  • Daniel January 10, 2013, 1:23 am

    If you count the characters, including white spaces you will see that they are the same until character 22 (u). Then starting with character 23 (the space after Himanshu) the text differs. Btw, the number in some text editors this is called column number.

  • Ash November 3, 2013, 7:25 pm

    4. Display bytes that differ

    If in the example 3 above we would also like to know the values of individual differing bytes being compared then the -l option can be used

    $ cmp -l -i 5:2 file1.txt file2.txt
    2 156 115
    3 141 171
    4 155 40
    5 145 156
    6 40 141
    7 151 155
    8 163 145
    10 110 151
    11 151 163
    12 155 40
    13 141 110
    14 156 151
    15 163 155
    16 150 141
    17 165 156
    18 12 163
    cmp: EOF on file1.txt

    can any one plz explain me what is meant by it ?
    2 156 115
    3 141 171
    4 155 40
    5 145 156
    6 40 141
    7 151 155
    8 163 145
    10 110 151
    11 151 163
    12 155 40
    13 141 110
    14 156 151
    15 163 155
    16 150 141
    17 165 156
    18 12 163