View Differences Between Files with diff

Written by Chris Gregg, with modifications by Nick Troccoli

Click here for a walkthrough video.

The diff program is useful for figuring out the differences between two files. In practice we often use it to compare two versions of a file (say, maybe your C code for an assignment) to determine what changed. Imagine you keep a backup of your assignment, change something, and suddenly it doesn't work at all--diff will help you figure out what changed by identifying any line(s) that don't match. It's also good for comparing output from your solution to a homework assignment with the output of a reference solution, to make sure your output matches exactly. Example:

$ diff hello.c hello2.c
5c5
<     printf("Hello, World!\n");
---
>     printf("HellO, World!\n");
$

The diff command output takes a bit of time to learn. In the above files, line 5 in both files (5c5) has a change. The first file is denoted by <, and the second file is denoted by >.

Another example:

$ diff hello.c hello3.c
3a4
> // a comment
5c6
<     printf("Hello, World!\n");
---
>     printf("HellO, World!\n");
$

In this case, line 3 from hello.c was not present and was "added" at line 4 in file 2. Then, we have the changed line, which happens at line 5 in hello.c and at line 6 in hello3.c.

If we re-run the command with the opposite order of the files, we get:

4d3
< // a comment
6c5
<     printf("HellO, World!\n");
---
>     printf("Hello, World!\n");
$

Diff always notes the differences with respect to the first file, so in this case, the comment line 4 was present in the first file but not the second file, so it was deleted (and would have been present in the second file at line 3).

Here are the three files we just looked at:

$ cat hello.c
#include<stdio.h>
#include<stdlib.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}
$ cat hello2.c
#include<stdio.h>
#include<stdlib.h>

int main() {
    printf("HellO, World!\n");
    return 0;
}
$ cat hello3.c
#include<stdio.h>
#include<stdlib.h>

// a comment
int main() {
    printf("HellO, World!\n");
    return 0;
}
$

For more information on the diff command, see here. Incidentally, the diff command works by solving the "longest common subsequence" problem that you might have seen in CS106B!