The cmp command is used to compare two files in Linux. It is mostly useful for scripts as it only reports whether the files are different or not. It does not report the differences. It can also be used to compare non-text files like libraries, images, music, etc.
cmp Command Basic Usage
By default, the
cmp command displays nothing if the files are the same. If they differ, the
cmp command displays the byte and line number at which the first difference occurs. Another command that displays the differences between files is the diff command.
cmp [options] file_1 file_2
If the files are identical,
cmp returns 0 as the exit status. If the files differ,
cmp returns 1 as the exit status.
Using the cmp command
Let’s now see the practical uses of the cmp command.
1. Compare two images
cmp img.png same_img.png
You can use the cmp command to compare images as
cmp compares the files byte-by-byte. Since images are also a stream of bytes, if the two streams are identical, it means that the images are identical. In that case, cmp will not report anything.
2. Compare backups with current file
cmp prog.o.bak prog.o
This compares prog.o.bak and prog.o . If the two files differ, cmp will print a message like:
prog.o.bak prog.o differ: char 4, line 1
Another output that may be produced is this.
cmp: EOF on prog.o.bak
If this is displayed, then a part of prog.o is identical to prog.o.bak , but there is additional data in prog.o.
3. Compare files in scripts
cmp reports if the two files given as arguments are different. This can be suppressed by using the
-s option. Let’s see how this can be useful in a short BASH script. This script relies on exit codes of cmp rather than whether it produces an output or not.
#!/bin/bash if cmp -s prog.c.bak prog.c then echo "These files are the same." fi
cmp gives an exit value of 0 if the files are identical, a value of 1 if different, or a value of 2 if an error occurs. As a result, this script will print “
These files are the same.” if the files are identical.
4. Compare plain text files with the cmp command
cmp essentially works on files that contain the text. But, named pipes, a feature provided by Linux can be used to directly compare text without the need for creating any files! This BASH trick can be useful for comparing long website links. So, if you don’t want to put a strain on your eyes matching the links or you are just lazy – use named pipes with
cmp <(echo "hello") <(echo "hello")
Since the two texts are identical,
cmp doesn’t print anything. Let’s now compare two unidentical texts.
cmp <(echo "hello") <(echo "hello hi")
cmp reports that difference exists.
Note: Earlier I mentioned that you don’t need to create files to compare text if you’re using named pipes.
Named pipes are actually internal files – ‘managed files’ created by BASH for you.
BASH does all the dirty work for you and you as a user don’t have to worry about creating external files to just compare text.
cmp is a concise yet useful tool to compare files. It is ubiquitous in the *NIX world and if complemented with other features like named pipes, can be used to save some time comparing plain text. I hope you learned something new about the
cmp command through this article. Let us know in the comments which use of
cmp command you didn’t already know about.