
- Unix Commands Reference
- Unix Commands - Home
cmp Command in Linux
The cmp command in Linux is a simple yet powerful tool used to compare two files byte by byte. It's a command-line utility that's part of the GNU diffutils package and is commonly used to check if two files are identical or to find the location of the first difference between them.
Table of Contents
Here's a comprehensive tutorial to understand the various options available with the cmp command and how to use them effectively.
- Understanding cmp Command
- Install cmp Command
- How to Use cmp Command in Linux?
- Manual Page of cmp command
- Examples of cmp Command in Linux
- Alternatives of cmp Command
Understanding cmp Command
The cmp command is a utility in Linux that allows users to compare two files byte by byte. It's a straightforward tool that reports the location of the first mismatch between the files, if any and is typically used to determine if two files are identical or not.
The exit status of the cmp command is 0 if the files are the same, 1 if the files are different, and 2 if there is trouble (such as an inability to open files or other errors). The SKIP values may be followed by multiplicative suffixes such as kB (1000), K (1024), MB (1,000,000), M (1,048,576), and so on for T, P, E, Z, Y. This allows for skipping larger portions of the files based on these units.
Install cmp Command
The cmp command is usually pre-installed on most Linux distributions as part of the coreutils package. If you find that it's not available on your system, or you wish to install the latest version, you can do so using your distribution's package manager.
For Ubuntu or Debian-based Systems
For Ubuntu or Debian-based systems, you can install coreutils using the followingn command −
sudo apt install coreutils
For CentOS or RHEL Systems
Similarly, for CentOS or RHEL, you can use the following command −
sudo yum install coreutils
For Arch Linux
For Arch Linux, the command would be −
sudo pacman -S coreutils
Once installed, you can use cmp by simply typing cmp file1 file2 in the terminal, replacing file1 and file2 with the actual file names you wish to compare. If the files are identical, cmp will not output anything; if they differ, it will indicate where the first difference occurs.
How to Use cmp Command in Linux?
For beginner usage, cmp offers options to skip a certain number of bytes from the beginning of the files or to compare up to a certain byte, among other features. These options can be particularly useful when dealing with large files or when you need to focus on specific sections of files.
The cmp command is an essential tool for system administrators, developers, and anyone who needs to perform file comparisons as part of their workflow. It's simple to use yet powerful in its capability to quickly identify differences between files.
The basic syntax of the cmp command is as follows −
cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]
FILE1 and FILE2 are the names of the files you want to compare. SKIP1 and SKIP2 are optional parameters that specify the number of bytes to skip at the beginning of each file.
Here's a breakdown of the different options available with cmp −
Options | Descriptions |
---|---|
b, --print-bytes | This option makes cmp display the differing bytes in the output. It's useful when you need to know exactly what the differences are between the two files. |
-i, --ignore-initial | With this option, you can skip a specified number of initial bytes from both files before the comparison starts. This is particularly useful when the files have headers or other data at the beginning that you want to exclude from the comparison. |
-l, --verbose | This option outputs the byte numbers and the differing byte values, providing a detailed account of the differences. |
-v, --version | Outputs version information and exits. |
-h, --help | Displays help information and exits. |
-n, --bytes | This limits the comparison to the first LIMIT bytes of the files. It's handy when you only want to compare a certain portion of the files. |
-s, --quiet, --silent | When this option is used, cmp suppresses all normal output. It's useful for scripts where you only care about the exit status but not the actual differences. |
Examples of cmp Command in Linux
For detailed examples and additional options, you can always refer to the cmp man page or online resources for comprehensive guides and tutorials.
Here are some examples of the cmp command in Linux showcasing different functionalities −
- Basic Comparison
- Displaying Differing Bytes
- Reporting All Differences
- Ignoring Initial Bytes
- Limiting Comparison
- Silent Comparison
Basic Comparison ( simply compare two files)
This compares the files file.txt and file1.txt byte. If the files are identical, cmp exits silently (exit code 0). If they differ, it prints a message indicating the first point of difference (e.g., file.txt file1.txt differ: byte 2, line 1) −
cmp file.txt file1.txt

Displaying Differing Bytes (compare two files and display the differing bytes)
This compares file.txt and file1.txt and, if they differ, displays the actual byte values that are different. This -b (or --print-bytes) option instructs cmp to display the actual differing bytes in ASCII or octal format when it finds a mismatch between the files −
cmp -b file.txt file1.txt

Note − This can be helpful for understanding the specific nature of the discrepancies. This helps you see the specific characters or byte values that are different.
Reporting All Differences
This instructs cmp to print the byte position and value for every difference it finds between file.txt and file1.txt, not just the first one. This provides a more detailed comparison report. This -l (or --verbose) option makes cmp more verbose in its output −
cmp -l file.txt file1.txt

Note − Instead of just reporting the location of the first difference, it prints the byte position and value for all the differing bytes encountered during the comparison.
Ignoring Initial Bytes (compare two files but ignore the first 10 bytes)
This compares file.txt and file1.txt, but it skips the first 10 bytes of each file before starting the actual comparison. This can be useful if the files have headers or other irrelevant data at the beginning. This option -i allows you to skip a certain number of bytes at the beginning of each file before starting the comparison −
cmp -i 10 file.txt file1.txt

Note − You can specify two values separated by a colon (:). SKIP1 defines the number of bytes to skip in the first file (FILE1), and SKIP2 defines the number to skip in the second file (FILE2). This can be useful if the files have headers or other irrelevant data at the beginning.
Limiting Comparison (compare the first 50 bytes of two files)
This compares only the first 50 bytes of file1.txt and file2.txt. cmp will stop comparing after reaching the 50th byte, regardless of whether a difference is found within those 50 bytes −
cmp -n 50 file.txt file1.txt

Note − This -n LIMIT (or --bytes=LIMIT) option sets a limit on the number of bytes to be compared in the files. cmp will stop comparing after reaching the specified byte limit (LIMIT), regardless of whether a difference is found or not. This is useful when you only need to compare a specific portion of the files.
Silent Comparison (compare two files silently and only check the exit status)
This example uses cmp with the -s (silent) option. The command itself doesn't produce any output, but the exit status is used in an if statement to determine if the files are the same (exit code 0) or different (non-zero exit code).
cmp -s file.txt file1.txt echo $?

This -s (or --quiet or --silent) option instructs cmp to suppress all normal output −
if cmp -s file.txt file1.txt; then echo "Files are identical" else echo "Files differ" fi

Note − Instead of printing messages about differences or success, it will only return an exit status that you can use in scripts to determine the comparison outcome.
These are just a few examples, and you can combine different options to achieve the desired comparison behavior.
Manual Page of cmp command
This man page displays a message explaining the usage and all available options for the cmp command −
man cmp

Alternatives of cmp Command
In the Linux environment, the cmp command is a useful utility for comparing two files byte by byte. However, there are several alternatives that can serve similar or more advanced purposes.
The diff Command
The diff command, for instance, is a powerful tool that not only compares files but also provides the differences in a detailed manner.
The vimdiff Command
For a more visual comparison, tools like vimdiff allow users to see differences side by side within the Vim editor. Other utilities such as md5sum or sha256sum can be used to compare checksums of files, which is a way to verify that files are identical without directly comparing the file contents.
The difftastic Command
For those looking for a structural comparison, difftastic is a syntax-aware diff tool that can compare files based on their syntax rather than just line by line. Each of these tools offers unique features and can be chosen based on the specific needs of the task at hand.
Conclusion
The cmp command is a versatile tool that can be used in various scenarios where file comparison is needed. Whether you're a system administrator, a developer, or just a curious user, understanding the cmp command and its options can help you perform file comparisons efficiently and effectively. For more detailed information, you can always refer to the cmp man page.