cmp Command Cheat Sheet
cmp (compare) is a utility for comparing two files of any type (text or binary) byte by byte. It is faster and simpler than diff when you just want to know if files differ or where the first difference is.
Synopsis
cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]
Description
cmp compares two files byte by byte.
- If files are identical, it is silent (prints nothing) and returns exit code 0.
- If they differ, it prints the byte and line number of the first difference and returns exit code 1.
- If one file is a prefix of the other (one ends early), it prints a message to stderr.
Basic Usage
Compare Two Files
cmp file1.bin file2.bin
Output if different:
file1.bin file2.bin differ: byte 9, line 1
Compare File with Standard Input
echo "hello" | cmp - file.txt
Options
-b, --print-bytes
Print the differing bytes.
cmp -b file1 file2
Output:
file1 file2 differ: byte 9, line 1 is 150 h 145 e
150) and character (h) from file1, and (145/e) from file2.
-l, --verbose
Output all differing bytes (byte number and octal values).
cmp -l file1 file2
Output:
9 150 145
10 145 154
Byte-Number Value1(Octal) Value2(Octal)
This is extremely useful for binary file analysis.
-s, --quiet, --silent
Suppress all normal output. Use only the exit code.
cmp -s file1 file2
Perfect for if statements in scripts.
-i, --ignore-initial=SKIP
Skip text/data at the beginning of files.
# Skip first 100 bytes of both
cmp -i 100 file1 file2
# Skip 100 bytes of file1 and 50 bytes of file2
cmp -i 100:50 file1 file2
-n, --bytes=LIMIT
Compare at most LIMIT bytes.
# Compare only the first 1KB
cmp -n 1024 file1 file2
Exit Codes
cmp exit codes are standardized and reliable for scripting.
| Code | Meaning |
|---|---|
| 0 | Files are identical |
| 1 | Files differ |
| 2 | Trouble (file not found, permission denied, etc.) |
Practical Examples
Check if ISO Download is Correct
After downloading image.iso, compare it against a verified copy or check if a burned disk matches the image.
cmp /dev/cdrom image.iso
Compare Header of Files
Check if two files have the same 10-byte header.
cmp -n 10 file1. header file2.header
Conditional Script Execution
Run a command only if configuration has changed.
if ! cmp -s config.conf config.conf.bak; then
echo "Config changed, restarting service..."
systemctl restart myservice
cp config.conf config.conf.bak
fi
Locating Binary Corruption
If two binaries should be the same but have a different checksum, finding where they differ can give a clue (e.g., specific offset might correspond to a metadata field).
cmp -l bad_file good_file | head -n 5
cmp vs diff
| Feature | cmp | diff |
|---|---|---|
| Type | Byte-by-byte | Line-by-line |
| Target | Binary & Text | Text |
| Speed | Faster | Slower |
| Output | First byte diff | Contextual text diff |
| Goal | Identity check | Content auditing |
Notes
- Byte counts are 1-based.
- Input can be from pipes/stdin (
-as filename). cmpessentially answers "Are these the same?" whilediffanswers "How are these different?".