When working in a Linux environment, you will often need to verify if two files are exactly identical. While tools like diff are excellent for showing human-readable changes in text files, they are less efficient for binary files or when you only need a simple “yes or no” verification.
The cmp (compare) command is designed specifically for this purpose. It compares two files byte by byte, making it incredibly fast, highly accurate, and perfect for verifying backups, checking for corruption, or comparing compiled binary files.
Understanding the cmp Command
Unlike text-based comparison tools, cmp does not care about lines, paragraphs, or formatting. It reads the raw data of both files starting from the very first byte.
The basic syntax is simple:
cmp file1.txt file2.txt
If the two files are completely identical, cmp will output absolutely nothing and return you to the command prompt. In Linux scripting terms, this means it returns a successful exit status (0).
If the files differ, cmp will immediately stop at the first difference it encounters and print the byte and line number where the mismatch occurred.
Example output for different files:
file1.txt file2.txt differ: byte 14, line 2
Practical Examples of Using cmp
The cmp command includes several flags that modify its output, making it adaptable for both quick manual checks and automated bash scripts.
1. Checking for Differences Silently
If you are writing a bash script and only care whether the files match (without printing any text to the terminal), use the -s (silent or quiet) flag.
cmp -s backup.tar.gz original.tar.gz
Because there is no terminal output, you must check the exit status using $? immediately after running the command:
- Exit status
0: The files are identical. - Exit status
1: The files are different. - Exit status
2: There was an error (e.g., a file could not be found or read).
You can use this directly in an if-statement:
if cmp -s fileA fileB; then echo "Matches"; else echo "Differs"; fi
2. Showing the Actual Differences
If you want to see exactly what bytes differ, rather than just the location of the first mismatch, use the -b (print bytes) flag.
cmp -b config1.conf config2.conf
The output will show the byte number, line number, and the differing characters represented in octal format and ASCII. For example:
config1.conf config2.conf differ: byte 45, line 4 is 141 a 145 e
This tells you that at byte 45, the first file has an “a” and the second file has an “e”.
3. Listing All Differences in a File
By default, cmp stops evaluating at the very first difference it finds to save processing time. If you want to see a complete list of every differing byte between the two files, use the -l (verbose/list) flag.
cmp -l image_v1.png image_v2.png
This will print a long list detailing every byte position where a mismatch occurs, along with the differing octal values. Be cautious using this on large files with many differences, as it will flood your terminal screen.
When to Use cmp vs diff
Choosing between cmp and diff depends entirely on the type of files you are comparing and the information you need.
- Use cmp when: You are comparing binary files (images, compiled programs, compressed archives), you only need to know if they differ, or you want the absolute fastest comparison possible for large files.
- Use diff when: You are comparing text files (source code, configuration files, text documents) and you need to see exactly which lines were added, removed, or modified in a human-readable format.
By incorporating the cmp command into your Linux workflow, you gain a highly efficient tool for verifying file integrity and detecting silent corruption in critical data.