Back
The cmp utility compares two files of any type 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.
Syntax and Options
cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]
3 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.
The cmp utility compares two files of any type 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.
Syntax and Options
cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]
3 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.