cmp Command
Beginner Text Processing man(1)Compare two files byte by byte
👁 13 views
📅 Updated: Mar 16, 2026
SYNTAX
cmp [OPTION]... FILE1 FILE2
What Does cmp Do?
The cmp command compares two files byte by byte. Unlike diff which compares text line by line, cmp works at the byte level, making it suitable for both text and binary file comparison.
cmp reports the first byte position and line number where the files differ. It is much faster than diff for simply checking whether two files are identical, as it stops at the first difference.
The command is commonly used in scripts to verify file integrity, compare binary files, check if a download completed correctly, and validate backup copies against originals.
cmp reports the first byte position and line number where the files differ. It is much faster than diff for simply checking whether two files are identical, as it stops at the first difference.
The command is commonly used in scripts to verify file integrity, compare binary files, check if a download completed correctly, and validate backup copies against originals.
Options & Flags
| Option | Description | Example |
|---|---|---|
| -l | Print all differing bytes with positions | cmp -l file1.bin file2.bin |
| -s | Silent mode — exit status only, no output | cmp -s file1 file2 && echo "Identical" |
| -i | Skip first N bytes | cmp -i 512 disk1.img disk2.img |
| -n | Compare at most N bytes | cmp -n 1024 file1 file2 |
| -b | Print differing bytes | cmp -b file1.txt file2.txt |
| exit codes | 0=identical, 1=different, 2=error | cmp -s a b; echo $? |
Practical Examples
#1 Compare two files
Reports the first byte where the files differ, or nothing if identical.
$ cmp original.bin backup.bin
Output:
original.bin backup.bin differ: byte 4521, line 23
#2 Silent comparison in script
Uses exit code to check if files are identical without any output.
$ if cmp -s config.yml config.yml.bak; then echo "No changes"; else echo "Modified"; fi#3 List all differences
Shows every byte position where the files differ with octal values.
$ cmp -l file1.bin file2.bin
Output:
4521 141 142\n4522 157 160
#4 Compare download integrity
Verifies a downloaded file matches the original.
$ cmp -s downloaded.iso original.iso && echo "OK" || echo "CORRUPT"#5 Compare first N bytes
Compares only the first 512 bytes (useful for checking boot sectors).
$ cmp -n 512 disk1.img disk2.img#6 Skip header bytes
Skips the 44-byte WAV header to compare only audio data.
$ cmp -i 44 audio1.wav audio2.wavTips & Best Practices
Use -s in scripts: cmp -s is the fastest way to check if two files are identical in shell scripts. It stops at the first difference and produces no output.
cmp vs diff: Use cmp for binary files and simple identity checks. Use diff for text files where you need to see what changed.
Large files: For very large files, cmp -s is much faster than diff because it only needs to find the first difference, not all differences.
Frequently Asked Questions
How do I check if two files are identical?
Use cmp -s file1 file2. If the exit code is 0 (check with echo $?), the files are identical. This works for both text and binary files.
What is the difference between cmp and diff?
cmp compares byte-by-byte and reports the first difference. diff compares line-by-line and shows all differences. cmp is faster for binary files and identity checks.
How do I compare binary files?
Use cmp file1.bin file2.bin for basic comparison, or cmp -l for a detailed byte-by-byte listing of all differences.
Related Commands
More Text Processing Commands
Master Linux with Professional eBooks
Curated IT eBooks covering Linux, DevOps, Cloud, and more
Browse Books →