$_ CLI In Depth
Learning progress 0%
lesson text viewing editors

Comparing Files: diff, cmp & comm

6 min read Module 3: Viewing Files & Text Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:05

Exit status first

All three tools return an exit status that a script can test. The convention is zero when the files are the same, one when they differ, and a value greater than one when the tool itself has a problem, such as a missing file. That convention is why if cmp -s a b is a clean test for identical contents. The option -s suppresses the output so only the status remains.

The exit status is what a script uses. The printed output is for people. When you write a script, decide first whether you need a yes or no answer, a list of changes, or a set relationship, and then choose the tool.

cmp: the first byte that differs

The command cmp compares two files byte by byte and stops at the first difference. It prints the byte offset and the line number where that difference occurs. It does not show the rest of the changes, so it is the fastest way to answer whether two files match and where they first diverge. For binary files it is the right tool, because a line-based diff makes little sense for data that has no lines.

BASH4 lines
cd /workspace
printf 'a\nb\nc\n' > old.txt
printf 'a\nB\nc\nd\n' > new.txt
cmp old.txt new.txt

The output reports that the files differ at character 3, on line 2, where the lowercase b in the old file meets the uppercase B in the new one.

diff: the changes between two files

The command diff prints the changes needed to turn the first file into the second. The default format is the normal diff format, which uses commands such as 2c2, meaning line two of the first file is changed into line two of the second, and 3a4, meaning after line three of the first file, line four of the second file is added. Each change lists the old lines with a less-than sign and the new lines with a greater-than sign.

BASH2 lines
cd /workspace
diff old.txt new.txt

The output shows the changed line and the appended line. The unified format, which the option -u selects, is easier for people to read in a patch. It shows a few lines of context around each change, with a plus sign for additions and a minus sign for removals. Patches for software use the unified format, because tools such as patch apply it reliably.

Directories and recursive comparison

The option -r compares two directories recursively, which is a way to see every change between a project and its copy. The option -q reports only which files differ, without the content. For very large trees, the combination of recursive comparison and a checksum tool is often faster, but diff gives the most readable result.

comm: sets from sorted lists

The command comm reads two files that are sorted in the same order and prints three columns. The first column holds the lines unique to the first file, the second holds the lines unique to the second, and the third holds the lines common to both. The options -1, -2, and -3 suppress columns. For example, comm -23 prints only the lines unique to the first file, which is a set difference.

The sorting requirement is strict. If the inputs are not sorted with the same collation, comm gives wrong results without any error. Use sort on both files first, and use the same sort options for both, as the lab does.

BASH4 lines
cd /workspace
sort old.txt > old.sorted
sort new.txt > new.sorted
comm -23 old.sorted new.sorted

The output is the line b, which appears in the old file and not in the new sorted list.

Tip

Use cmp -s in a script that only needs a yes or no answer. Use diff when you need to show a person what changed, and use comm when the question is about membership in two lists.

Comparing output without temporary files

Many shells let you compare the output of two commands directly, using a process substitution such as diff <(command1) <(command2). The shell gives each command a file-like name, so diff reads them as if they were files. This avoids temporary files, but it is a bash feature, not a POSIX one, so it belongs in bash scripts rather than in portable ones.

Comparing whole directories

To compare two trees, the recursive option of diff prints the changed files along with their differences. Adding -q reduces the output to the names of the files that differ, and a single line for each file that exists in only one tree. This is the right view when you want to know what changed rather than how. A common command is diff -rq old new, which lists the differences without printing each line.

Read the exit status of the directory comparison the same way as for a file: zero means identical trees, and one means at least one difference. Because the comparison is by content, a file that changed only its timestamp is not reported, which is the behavior you want for a backup check.

When the trees are large, a checksum of every file is often faster than a full textual diff, but a diff gives the detail that a checksum cannot. Choose the tool according to the question you are asking.

Reading a diff you did not write

When you review a change someone else made, start with the summary, then read the hunks in order. Each hunk begins with a header that gives the line ranges in the old and new files. A line that starts with a minus sign existed only in the old file, a line with a plus sign exists only in the new file, and an unmarked line is context that is the same in both. Reading the context tells you what the change is attached to, which is often more important than the change itself.

Be wary of whitespace-only changes, which create large diffs that hide the real edit. diff has options that ignore whitespace, and they are useful for review, although the final file must still be checked in its exact form before it is used.

Practice in the lab

The lab creates two versions of a small file, then asks you to save the diff, the first byte difference reported by cmp, and the lines unique to the first file using comm. Each answer is written to a file for the checker.

Characters to bytes: UTF-8 and line endings

Visualizer

Files hold bytes. See how a code point becomes UTF-8 bytes, and why a Windows line ending breaks shell scripts.

1 / 4
character
é
code point
U+00E9
decimal
233

Unicode gives é the number 0xE9. It is above 0x7F, so it cannot be a single ASCII byte.

printf "\xc3\xa9\n" | od -An -tx1 -c
Terminal lab · 3 steps · real Linux in your browser

Compare two versions of a file

Save a diff, find the first differing byte with cmp, and list the lines unique to the old file with comm.

01
Save the diff
Save the normal diff of old.txt and new.txt. diff exits with status 1 when the files differ, which is expected: ``bash diff old.txt new.txt > changes.diff ``
Show hint for step 1
The diff output has a change command such as 2c2, then the old and new lines.
02
Find the first difference
Save the message that cmp prints for the two files: ``bash cmp old.txt new.txt > cmp.txt 2>&1 ``
Show hint for step 2
cmp stops at the first byte that differs and reports its position.
03
List lines unique to the old file
comm needs sorted input, so sort both files first. Then save the lines that appear only in the old file: ``bash sort old.txt > old.sorted sort new.txt > new.sorted comm -23 old.sorted new.sorted > only-old.txt ``
Show hint for step 3
-23 suppresses the columns for common lines and for lines unique to the second file.
Check yourself Question 1 of 4
Concept Check Exit status for identical files recall

What exit status does cmp -s return when two files are identical?

Recap & Key Takeaways

cmp reports the first byte that differs, which is the fastest test for identical contents.
diff prints the changes between files in normal or unified format, and it is the right tool for patches and reviews.
comm compares two sorted files into three columns, and both inputs must be sorted with the same collation.
Exit status zero means the files are the same, which makes these tools reliable in scripts.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue