How to Use the Linux Command cmp and Its 5 Options

The Linux command cmp is primarily used to compare two files to identify differences. It can be used for both text files and binary files, and it is particularly efficient when comparing large files. In this post, we’ll explain the basic usage of the cmp command, explore its key options, and share some practical use cases.

What is the Linux Command cmp?

The cmp command in Linux is a tool that compares two files byte by byte and identifies the first location where they differ. By default, cmp continues to compare the files until a difference is found, at which point it outputs the position and the difference. If the files are identical, cmp exits without any output and returns an exit code of 0.

Basic Usage of the Linux Command cmp

The simplest way to use the cmp command is as follows:

cmp file1 file2
ShellScript

For example, to compare two text files, file1.txt and file2.txt, you would enter:

cmp file1.txt file2.txt
ShellScript

If the files are identical, no message will be displayed, as shown below.

Figure 1. Linux Command cmp: Execution result when two files are identical
Figure 1. Linux Command cmp: Execution result when two files are identical

Next, let’s modify the b.txt file by changing the uppercase letters C and E to lowercase c and e.

Figure 2. Differences between the contents of a.txt and b.txt
Figure 2. Differences between the contents of a.txt and b.txt

Now that b.txt has been altered, information about the first difference will be displayed, as shown below.

Figure 3. Linux Command cmp: Execution result when two files are different
Figure 3. Linux Command cmp: Execution result when two files are different

cmp Command Options

The cmp command offers several useful options that can make file comparison tasks more efficient.

-l Option: Display Byte-by-Byte Differences

The -l option allows you to output the differences between two files on a byte-by-byte basis. This option is particularly useful when the files differ in multiple places.

cmp -l file1.txt file2.txt
ShellScript

When you run this command, it shows the differences in bytes between the two files, along with their positions in octal format. You can see that the 5th byte differs between 103 (C) and 143 (c), and the 9th byte differs between 105 (E) and 145 (e).

Figure 4. Linux Command cmp: Displaying differences in octal format with the -l option
Figure 4. Linux Command cmp: Displaying differences in octal format with the -l option

However, the above output might be challenging for most people to understand. The following option provides a clearer view.

-b Option: Show Different Characters

The -b option not only shows the byte and line where the difference occurs but also displays the differing character and its octal value.

cmp -b file1.txt file2.txt
ShellScript

As shown below, it only reports the first differing byte.

Figure 5. Linux Command cmp: Displaying different characters with the -b option
Figure 5. Linux Command cmp: Displaying different characters with the -b option

By combining the -l and -b options, you can clearly identify the exact location and character where the difference occurs.

Figure 6. Linux Command cmp: Combining -b and -l options to verify the location and character of differences
Figure 6. Linux Command cmp: Combining -b and -l options to verify the location and character of differences

-s Option: Only Check for Differences

The -s option allows cmp to only check whether the two files are identical without printing any output. This option is useful when you simply want to verify the equality of files.

cmp -s file1.txt file2.txt
ShellScript

After running this command, you can determine whether the files are identical by checking the exit code. As shown below, an exit code of 0 indicates that the files are the same.

Figure 7. Linux Command cmp: No output with the -s option, but exit code indicates identical files
Figure 7. Linux Command cmp: No output with the -s option, but exit code indicates identical files

If the exit code is 1, you can confirm that the files differ.

Figure 8. Linux Command cmp: No output with the -s option, but exit code indicates different files
Figure 8. Linux Command cmp: No output with the -s option, but exit code indicates different files

-n Option: Limit the Number of Bytes Compared

The -n option limits the comparison to a specified number of bytes. For example, if you only want to compare the first 100 bytes, you can enter:

cmp -n 100 file1.txt file2.txt
ShellScript

This command compares only the first 100 bytes of the two files and outputs the result. As shown below, the characters are identical up to the 3rd byte, with differences starting at the 5th byte.

Figure 9. Linux Command cmp: Limiting the comparison to a specified number of bytes with the -n option
Figure 9. Linux Command cmp: Limiting the comparison to a specified number of bytes with the -n option

-i Option: Specify the Start Position for Comparison

The -i option allows you to start comparing files from a specified byte position. This is useful when you only want to compare a portion of the files starting from a certain point.

cmp -i 100 file1.txt file2.txt
ShellScript

This command starts comparing the two files from the 100th byte. Interestingly, the reference point changes based on the specified byte. In the example below, when the comparison starts from the 8th byte, the location is reported as byte 1, but when starting from the 7th byte, it is reported as byte 2. The minimum value for the -i option is 0, so if you use -i 8, the comparison actually starts from the 9th byte. You should experiment with this value to see how it works.

Figure 10. Linux Command cmp: Setting the starting byte position for comparison with the -i option
Figure 10. Linux Command cmp: Setting the starting byte position for comparison with the -i option

Practical Uses of the cmp Command

Comparing Large Files

The cmp command is especially useful for comparing large files. It allows you to quickly verify whether the contents of the files are identical and pinpoints the exact location of any differences. This is particularly effective when comparing large binary files or log files.

Using in Scripts

Since the cmp command returns an exit code, you can automate file equality checks within scripts. For example, you can write a script to perform certain actions only if the files are different.

#!/bin/bash

if cmp -s a.txt b.txt; then
    echo "The files are identical."
else
    echo "The files are different."
    cat b.txt
fi
ShellScript

If the two files differ, the script will output “The files are different.” and display the contents of b.txt using the cat command.

Figure 11. Using the Linux command cmp in a shell script
Figure 11. Using the Linux command cmp in a script

Comparing Binary Files

The cmp command can be used to compare not just text files but also binary files. When comparing binary files, the -l option can be particularly useful for analyzing differences byte by byte.

Precautions

When using the cmp command, keep in mind that comparing very large files may take a significant amount of time. Additionally, since cmp compares the entire contents of both files by default, if you only want to compare specific portions of the files, make sure to use the -n or -i options appropriately.

Also, because the cmp command primarily compares files at the byte level, differences in line endings (e.g., between Windows and Unix) may be detected as differences. In such cases, a line-based comparison tool like the diff command might be more suitable.

Summary

The Linux command cmp is an extremely useful tool for comparing files in a Linux environment. It allows you to quickly and accurately identify differences at the byte level and offers various options to control the scope and output of the comparison. It performs particularly well when comparing large or binary files and can be integrated into scripts for automation. When you need to compare files, make sure to utilize the Linux command cmp effectively.

References

Leave a Comment