cmp

byte-by-byte file comparison

TLDR

Compare two files
$ cmp [file1] [file2]
Silent comparison (exit code only)
$ cmp -s [file1] [file2]
Show all differences
$ cmp -l [file1] [file2]
Print differing bytes
$ cmp -b [file1] [file2]
Compare first N bytes
$ cmp -n [1024] [file1] [file2]
Skip bytes at start
$ cmp -i [100] [file1] [file2]

SYNOPSIS

cmp [option]... file1 [file2 [skip1 [skip2]]]

DESCRIPTION

cmp compares two files byte by byte and reports the location of the first difference. By default, it prints the byte number and line number where the files diverge, or produces no output if they are identical.Unlike diff, which operates on text lines, cmp works at the byte level, making it more efficient and appropriate for binary files. The -s (silent) mode is commonly used in scripts where only the exit status matters: 0 for identical files, 1 for differences, and 2 for errors.The -l option lists all byte positions where the files differ, along with the differing byte values in octal. The tool can also skip initial bytes in one or both files and limit the comparison to a specified number of bytes.

PARAMETERS

-b, --print-bytes

Print differing bytes
-i skip, --ignore-initial skip
Skip first bytes of both files
-i skip1:skip2
Skip different amounts per file
-l, --verbose
Output all byte differences
-n limit, --bytes limit
Compare at most limit bytes
-s, --quiet, --silent
Suppress output, exit code only
--help
Display help
-v, --version
Show version

EXIT STATUS

0: Files are identical1: Files differ2: Error occurred

SKIP SUFFIXES

K (1024), M (1048576), G (1073741824)kB (1000), MB (1000000), GB (1000000000)

INSTALL

sudo apt install diffutils
sudo dnf install diffutils
sudo pacman -S diffutils
sudo apk add diffutils
sudo zypper install diffutils
brew install diffutils
nix profile install nixpkgs#diffutils

CAVEATS

Cannot compare directories, only files. For text file differences, use diff instead. Reading from stdin use - as filename.

SEE ALSO

diff(1), comm(1), md5sum(1)

RESOURCES

Source code · Documentation