On Unix/Linux, which command compares two files byte by byte to report the first difference or confirm they are identical?

Difficulty: Easy

Correct Answer: cmp

Explanation:


Introduction / Context:
File comparison is a routine task in system administration and software development. While 'diff' shows line-by-line textual changes, sometimes you need a raw, byte-level comparison to validate copies, downloads, or binary artifacts. Unix provides a minimalist tool for this exact purpose.


Given Data / Assumptions:

  • You have two files that may be identical or differ in binary content.
  • You need a simple pass/fail or the first differing location.
  • Human-friendly patches are not required.


Concept / Approach:
The 'cmp' command compares files byte by byte. With no options, it is silent if files match and reports the first byte/line where they differ if not. It is ideal for verifying integrity after copies, especially for binaries, images, and archives where textual diffs are not meaningful.


Step-by-Step Solution:

Run: cmp file1.bin file2.binIf no output appears, files are identical (exit status 0).If different, output includes byte and line of the first mismatch.Use 'cmp -s' for a quiet exit-status-only check in scripts.


Verification / Alternative check:
Compare 'sha256sum file1 file2' to confirm identical hashes. When both 'cmp' and hash agree, integrity is assured.


Why Other Options Are Wrong:

  • comp: DOS/Windows utility, not the standard Unix tool.
  • do / ccp: Not file comparison commands on Unix/Linux.
  • None of the above: Incorrect because 'cmp' is correct.


Common Pitfalls:
Using 'diff' for binaries (noisy/less useful), or forgetting that 'cmp' stops at the first difference (not a full difference report).


Final Answer:
cmp

More Questions from Unix

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion