Compare Exact File Bytes

Compare Exact File Bytes

The teacher copied a report and wants to check whether the copy has exactly the same bytes. That is a slightly different question from asking which lines changed. cmp compares the files byte by byte, so it can detect differences in data that a line-oriented explanation is not designed to summarize.

Let's begin with equal files and the silent option. Silent means the description is suppressed, not that the result disappears. The exit status still tells the shell whether the contents matched.

What you’ll learn

  • Compare two files byte by byte with cmp.
  • Use silent comparison and inspect its exit status.

Equal-looking names are not enough for equality

  1. First file

    The saved report supplies the bytes to compare.

    first.txt: 72
  2. Second file

    The copy initially has identical content.

    second.txt: 72
  3. Check

    cmp -s suppresses the description; status 0 signals equal contents.

    Same bytes
Choose cmp for byte equality and diff for an explanation of text changes.

Check whether a copied report has identical bytes

Prepare the example

Start in a new practice folder. We will create one small report and copy it to a second filename. That preparation gives us a known equal pair, rather than asking you to guess whether two existing files happen to match.

Bash · Linux terminal

printf '72\n' > first.txt
cp first.txt second.txt
cmp -s first.txt second.txt && printf 'Same bytes\n'

Expected result

Same bytes

The first line writes 72 to first.txt, and cp makes second.txt from those same bytes. cmp -s first.txt second.txt checks equality without printing its usual difference description. Because the check succeeds, && allows the message Same bytes to print.

If you change one input, the equality condition no longer succeeds and that message does not print. Inspect the exit status to distinguish a mismatch from a trouble condition such as an unreadable input. This kind of check is useful for verifying a copied file, but it does not explain a text edit as clearly as a line-oriented comparison.

Compare two byte streams

cmp compares two files byte by byte. Unlike diff, which is designed around text lines, cmp is useful when you simply need to know whether the underlying file data differs.

What is cmp?

cmp stands for "compare." It compares two files byte by byte and tells you the first point where they differ. If the files are identical, it says nothing. That’s right—it stays quiet. Silence means success!

cmp syntax for comparing two files

cmp file1 file2

Let’s try it out with some examples!

Step 1: Create Two Files

First, let’s create two simple text files for comparison:

echo "Hello world!" > fileA.txt
echo "Hello world!" > fileB.txt

Now let’s compare them:

cmp fileA.txt fileB.txt

Expected output for this example:


(no output — which means they are exactly the same!)

Step 2: Change One File

Now, let’s make a small change to fileB.txt:

echo "Hello world!!!" > fileB.txt

Try the compare command again:

cmp fileA.txt fileB.txt

Expected output for this example:


fileA.txt fileB.txt differ: byte 13, line 1

This tells us that the difference starts at byte 13, on line 1. Simple and precise!

Detailed and silent comparison results

Use -l to Show Byte Differences

If you want to see what exactly is different in terms of byte values, use the -l option:

cmp -l fileA.txt fileB.txt

Expected output for this example:


13 41 41
14 0 41
15 0 41

This tells you the byte position and the differing byte values in octal.

Use -s for Silent Mode

If you just want to know whether the files differ, and don’t care where, use the silent flag:

cmp -s fileA.txt fileB.txt

This will give no output if the files are the same, and exit silently. But you can check the result using:

echo $?

Expected output for this example:


0   (means files are the same)
1   (means files are different)

Use cmp when byte-level equality matters

  • cmp file1 file2 — Basic comparison
  • cmp -l — Show byte-by-byte differences
  • cmp -s — Silent mode for scripting

Try it, then check the worked answer

Your task: Check exact equality without printing a difference description.

Start in a new practice folder. We will create one small report and copy it to a second filename. That preparation gives us a known equal pair, rather than asking you to guess whether two existing files happen to match.

Bash · complete example for this question

printf '72\n' > first.txt
cp first.txt second.txt
cmp -s first.txt second.txt && printf 'Same bytes\n'

Expected result

Same bytes

Worked answer

cmp -s first.txt second.txt

Use cmp -s for the exact equality check without a difference description. Both files existing is not enough: their bytes must match. An equality check is about content, not simply names, sizes or whether each path can be found.

Check your decision

Check exact equality without printing a difference description.
Choose one answer. You can try again.

Answer and explanation

cmp -s first.txt second.txt

Use cmp -s for the exact equality check without a difference description. Both files existing is not enough: their bytes must match. An equality check is about content, not simply names, sizes or whether each path can be found.

Use cmp status to distinguish equality from failure

Choose cmp for byte equality and diff for an explanation of text changes. With silent comparison, deliberately check the status because an empty terminal is not the whole result. Compare readable inputs and distinguish different contents from a comparison that could not run successfully.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Inspect a File's Likely Type →