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.
Table of Contents
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
ShellScriptFor example, to compare two text files, file1.txt
and file2.txt
, you would enter:
cmp file1.txt file2.txt
ShellScriptIf the files are identical, no message will be displayed, as shown below.

cmp
: Execution result when two files are identicalNext, let’s modify the b.txt
file by changing the uppercase letters C
and E
to lowercase c
and e
.

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

cmp
: Execution result when two files are differentcmp
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
ShellScriptWhen 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).

cmp
: Displaying differences in octal format with the -l
optionHowever, 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
ShellScriptAs shown below, it only reports the first differing byte.

cmp
: Displaying different characters with the -b
optionBy combining the -l
and -b
options, you can clearly identify the exact location and character where the difference occurs.

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
ShellScriptAfter 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.

cmp
: No output with the -s
option, but exit code indicates identical filesIf the exit code is 1, you can confirm that the files differ.

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
ShellScriptThis 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.

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
ShellScriptThis 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.

cmp
: Setting the starting byte position for comparison with the -i
optionPractical 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
ShellScriptIf the two files differ, the script will output “The files are different.” and display the contents of b.txt
using the cat
command.

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.