cmp — Byte-wise comparison
Linux / Basic Linux commands / cmp.html
Compares two files byte by byte and reports the first difference.
Basic Information
| Command Name | cmp |
|---|---|
| One-line Introduction | Compares two files byte by byte and reports the first difference. |
| Provided by | GNU diffutils |
| Official Documentation | https://man7.org/linux/man-pages/man1/cmp.1.html |
| Supported Operating Systems | Linux, macOS, BSD, WSL |
| Installation Status | Installed by default |
| Category | Linux basic commands (tags: Linux basic commands (tags: cmp, compare, byte, diff)) |
What is this command?
cmp compares two files byte by byte from the start and reports the first differing offset and values. This differs from diff, which shows text differences by line.
The -s option returns only an exit status, which suits scripts that test equality. It is especially useful for binary or compressed files.
What problems does it solve?
- Verifying a copied file matches the original
- Comparing binaries or compressed files
- Testing equality in scripts
- When line-based diff is not decisive
Key features
First difference
Reports the offset and values of the first difference.
- When to use: Finding where files diverge
- What you get: It pinpoints the byte precisely.
- Limitations: It does not list every difference at once.
Quiet comparison
The -s flag suppresses output and returns a status.
- When to use: When scripts only need equality
- What you get: Branching becomes simple.
- Limitations: You cannot see what differs.
Length checks
It also reports when lengths differ.
- When to use: Detecting truncated files
- What you get: Transfer problems surface quickly.
- Limitations: Equal content with different length is rare.
Installation and usage
Check whether it is installed
cmp --version
sudo apt install diffutils # Debian/Ubuntu sudo dnf install diffutils # Fedora
Real-world examples
Byte comparison
Show the first difference.
cmp a.bin b.bin
Quiet comparison
Get equality via exit status only.
cmp -s a.bin b.bin && echo SAME || echo DIFF
Compare archives
Check whether archives match.
cmp -s out1.tar.gz out2.tar.gz; echo $?