cmp Command in Linux with Examples

This tutorial explains Linux “cmp” command, options and its usage with examples.

cmp – compare two files

DESCRIPTION

Compares two files and tells you what line numbers are different.

SYNOPSIS

cmp [-c] [-i N] [-l] [-s] [-v] firstfile secondfile

OPTIONS :

advertisement
advertisement

-c
Output differing bytes as characters.
-i N
Ignore differences in the first N bytes of input.
-l
Write the byte number (decimal) and the differing bytes (octal) for each difference.
-s
Write nothing for differing files; return exit statuses only.
-v
Output version info.
firstfile
First file that you wish to compare.
secondfile
Second file that you wish to compare to.

Return values

0 — files are identical
1 — files differ
2 — inaccessible or missing argument

EXAMPLES

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

$ cat file2.txt
My name is Mohak Kataria
$ cat file1.txt
My name is Mohak

1. Compare file1 to file2 and outputs results

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

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

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

So we see that the initial 5 bytes were skipped

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

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

4. Display bytes that differ

advertisement
$ cmp -l -i 5:2 file1.txt file2.txt 
 1 155  40
 2 145 156
 3  40 141
 4 151 155
 5 163 145
 7 115 151
 8 157 163
 9 150  40
10 141 115
11 153 157
12  12 150
cmp: EOF on file1.txt

5. Upper limit on number of bytes to compare

$ cmp -l -n 10 -i 5:2 file1.txt file2.txt 
 1 155  40
 2 145 156
 3  40 141
 4 151 155
 5 163 145
 7 115 151
 8 157 163
 9 150  40
10 141 115

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.

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.