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.
cd /workspace
printf 'a\nb\nc\n' > old.txt
printf 'a\nB\nc\nd\n' > new.txt
cmp old.txt new.txtThe 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.
cd /workspace
diff old.txt new.txtThe 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.
cd /workspace
sort old.txt > old.sorted
sort new.txt > new.sorted
comm -23 old.sorted new.sortedThe output is the line b, which appears in the old file and not in the new sorted list.
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.