The cmp Command on Linux (With Easy Examples)

Cmp Command

The cmp command is used to compare two files on Linux and Unix. It is mostly useful for scripts, as it only reports whether the files are different or not. It does not report the differences. Likewise, it can also be used to compare non-text files like libraries, images, music, etc. This command should work on any distribution such as Ubuntu, Fedora or OpenSUSE.

cmp Command Basic Usage

By default, the cmp command displays nothing if the content of 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
Cmp Files
Comparing Files in the command line

If the two files are identical, cmp returns 0 as the exit status. If the files differ, cmp returns 1 as the exit status.


Usage and examples of 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

By default, 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 / Shell 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. To use the command outside a script in order to list the differing bytes, you can use the command like this by specifying it with the -l flag:

cmp -l [file1] [file2]

This compares the files and also displays the byte number in the command-line output.


4. Compare two plain text with the cmp command in Linux

cmp utility 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.

cmp <(echo "hello") <(echo "hello")
Cmp Hello
Comparing Plain Text

Since the two texts are identical, cmp doesn’t print anything. Let’s now compare two nonidentical texts.

cmp <(echo "hello") <(echo "hello hi")
Cmp Hello Different
Comparing Plain Text

As expected cmp reports the differences between two text inputs.

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.


Conclusion: Comparing two files easily in the shell

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.

References

CMP command official Man Page