This tutorial explains Linux “cmp” command, options and its usage with examples.
DESCRIPTION
Compares two files and tells you what line numbers are different.
SYNOPSIS
cmp [-c] [-i N] [-l] [-s] [-v] firstfile secondfile
OPTIONS :
-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
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
$ 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
$ 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.
- Check Information Technology Books
- Practice Programming MCQs
- Apply for Programming Internship
- Check Linux Books