Illustration of a terminal window on Linux
Fatmawati Achmad Zaenuri/Shutterstock.com

Need to see the differences between two revisions of a text file? Then diff is the command you need. This tutorial shows you how to use diff on Linux and macOS, the easy way.

Diving into diff

The diff command compares two files and produces a list of the differences between the two. To be more accurate, it produces a list of the changes that would need to be made to the first file, to make it match the second file. If you keep that in mind you’ll find it easier to understand the output from diff. The diff command was designed to find differences between source code files and to produce an output that could be read and acted upon by other programs, such as the patch command. In this tutorial, we’re going to look at the most useful human-friendly ways to use diff.

Let’s dive right in and analyze two files. The order of the files on the command line determines which file diff considers to be the ‘first file’ and which it considers to be the “second file.” In the example below alpha1 is the first file, and alpha2 is the second file. Both files contain the phonetic alphabet but the second file, alpha2, has had some further editing so that the two files are not identical.

We can compare the files with this command. Type diff, a space, the name of the first file, a space, the name of the second file, and then press Enter.

diff alpha1 alpha2

Output from diff command with no options

How do we dissect that output? Once you know what to look for it’s not that bad. Each difference is listed in turn in a single column, and each difference is labeled. The label contains numbers either side of a letter, like 4c4. The first number is the line number in alpha1, and the second number is the line number in alpha2.  The letter in the middle can be:

  • c: The line in the first file needs to be changed to match the line in the second file.
  • d: The line in the first file must be deleted to match the second file.
  • a: Extra content must be added to the first file to make it match the second file.

The 4c4 in our example tell us that line four of alpha1 must be changed to match line four of alpha2. This is the first difference between the two files that diff found.

Lines that begin with < refer to the first file, in our example alpha1, and lines that start with > refer to the second file, alpha2. The line < Delta tells us that the word Delta is the content of line four in alpha1. The line > Dave tells us that the word Dave is the content of line four in alpha2. To summarise then, we need to replace Delta with Dave on line four in alpha1, to make that line match in both files.

The next change is indicated by the 12c12. Applying the same logic, this tells us that line 12 in alpha1 contains the word Lima, but line 12 of alpha2 contains the word Linux.

Read the remaining 47 paragraphs

Category: 
Date: 
Monday, April 15, 2019 - 8:00am