Linux Command Reference ยท Viewing and Searching Files
Linux cmp command
Reference for the Linux cmp command: compare two files byte by byte, find the first difference, list all differing bytes, skip offsets and use the exit status.
cmp compares two files byte by byte. If they are identical it prints nothing and exits with status 0. If they differ it prints the position of the first difference and exits with status 1. It works on any kind of file, text or binary, and it stops at the first difference unless you ask for more, so it is the fastest way to answer "are these two files the same?".
Unlike diff, cmp knows nothing about lines or hunks. It reports a byte offset and a line number, nothing else.
cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
cmp [-l | -s] [-b] [-i SKIP[:SKIP2]] [-n LIMIT] FILE1 [FILE2] cmp FILE1 FILE2 SKIP1 SKIP2 # older positional form
If FILE2 is omitted or is -, standard input is used.
| Option | Description |
|---|---|
-l |
List every differing byte: its 1-based offset, then both values in octal. |
-s |
Silent: print nothing, only set the exit status. Also --quiet. |
-b |
Print the differing bytes as characters next to the offset. |
-i N |
Skip the first N bytes of both files. -i N:M skips N in the first and M in the second. |
-n N |
Compare at most N bytes. |
Returns: 0 when identical (within the compared range), 1 when different, 2 when something went wrong (for example a file does not exist).
Edge case: when one file is a prefix of the other, cmp prints cmp: EOF on short.txt after byte 5 and returns 1.
Usage notes
- Fast exit. Without
-l,cmpstops at the first difference, so comparing two huge files that differ near the start is nearly instant. - Byte offsets start at 1, and the message also gives a line number, counted from the newlines in the first file.
- Binary files.
cmpis the right tool for images, archives and executables, where line-oriented tools are meaningless. - Same file twice. If both names refer to the same file (a hard link or the same path)
cmpcan answer immediately. - Scripting. Use
cmp -s a binifconditions. Do not capture its output to decide equality; the exit status is the answer. - Compare with a checksum (
sha256sum) when the files are on different machines;cmpneeds both files locally. - Sparse and device files:
cmpreads the content as the system presents it, so comparing a disk image to a device (cmp image.iso /dev/sr0) verifies a burned medium.
Related
diff,comm,md5sumandsha256sum- Lessons: diff, comm and patch