Linux cmp command tutorial for beginners (7 examples)
On this page
- cmp command
- 1. How to compare two files using cmp
- 2. How to make cmp print differing bytes
- 3. How to make cmp skip some initial bytes from both files
- 4. How to make cmp display byte position (and value) for all differing bytes
- 5. How to limit number of bytes to be compared
- 6. How to display progress meter while using cmp command
- 7. How to make 'cmp' suppress output
Pretty much regardless of your role, if your regular work involves doing stuff on the command line in Linux, you may find yourself in a situation where-in you'd want to compare two files using a command line utility. There are several command line tools that let you do this, and one among them is the 'cmp' command.
In this tutorial, we will discuss 'cmp' through easy to understand examples. But before we do that, it's worth mentioning that all examples and instructions mentioned in this tutorial have been tested on Ubuntu 16.04LTS.
The 'cmp' command lets you carry out byte-by-byte comparison of two files. The utility provides several features in the form of command line options. In this tutorial, we will discuss some of the key options that'll give you (a beginner) a good idea about how the tool works.
1. How to compare two files using cmp
In its simplest form, you can use 'cmp' in the following way:
cmp [file1-name] [file2-name]
As already mentioned, the tool compares two files byte by byte. As a difference is found, the tool stops and produces an output that tells which line and byte the difference is in.
cmp file1.txt file2.txt
As you can see in the above screenshot, the output clearly says the files differ at byte 20 in line 1.
2. How to make cmp print differing bytes
If you want, you can also make 'cmp' display the differing bytes in its output. This can be done using the -b command line option.
cmp -b [file1] [file2]
cmp -b file1.txt file2.txt
In this case, as you can see in the screenshot above, the difference is in the 17th byte, which is 'l' in file1.txt and 'i' in file2.txt. The values 154 and 151 are the values for these bytes, respectively.
3. How to make cmp skip some initial bytes from both files
If you want, you can also make 'cmp' skip a particular number of initial bytes from both files, and then compare them. This can be done by specifying the number of bytes as argument to the -i command line option.
cmp -i [bytes-to-be-skipped] [file1] [file2]
cmp -i 10 file1.txt file2.txt
Note that in cases like these (where you use -i to skip bytes), the byte at which the comparison begins is treated as byte number zero.
Moving on, the tool also allows you to skip different number of bytes from both files. This can be done in the following way:
cmp -i [bytes-to-skip-from-first-file] : [bytes-to-skip-from-second-file] [file1] [file2]
cmp -i 4:7 file1.txt file2.txt
4. How to make cmp display byte position (and value) for all differing bytes
If you want, you can also make the 'cmp' command print byte position and byte value for all differing bytes. This feature can be accessed using the -l command line option.
cmp -l [file1] [file2]
cmp -l file1.txt file2.txt
The first column in the output (example shown above) represents the position (byte number) of differing bytes. The second column represents the byte value of the differing byte in the first file, while the third column represents the byte value of the differing byte in the second file.
5. How to limit number of bytes to be compared
The tool also allows you to limit the number of bytes that you want to compare - like, you may want to compare at most 25 or 50 bytes. This can be done by using the -n command line option.
$ cmp -n [number-of-bytes-to-be-compared] [file1] [file2]
$ cmp -n 25 file1.txt file2.txt
6. How to display progress meter while using cmp command
While comparing large files (or even partitions) using 'cmp', you may want to see the progress of the ongoing comparison. This can be done using the 'pv' command along with the 'cmp' command. Here's the command template that you can use:
$ pv [file1] | cmp -l [file2] > [output-file]
$ pv file1.txt | cmp -l file3.txt > output.txt
Note that the file 'output.txt' will contain all the output that 'cmp' command produces. The progress meter (that you can see in the above screenshot) is produced by the 'pv' command.
The 'pv' command usually doesn't come pre-installed in Linux (it doesn't on Ubuntu at-least). But you can easily install it using the following command:
sudo apt-get install pv
7. How to make 'cmp' suppress output
The tool also allows you to suppress the output it produces normally. This can be done using the -s command line option.
$ cmp -s [file1] [file2]
$ cmp -s file1.txt file2.txt
This option may come in handy when using the 'cmp' utility in scripts. For example, depending on whether the files are identical or not (which one can tell by accessing the command's exit code), you might want to display a custom message instead of the output the tool normally produces.
We've discussed most of the 'cmp' command options in this article, so you just need to practice these in order to start using the tool in your day-to-day work. In case of any doubt or query, refer to the 'cmp' man page.