Get more control over how file differences are found and displayed with some lesser-known options, and other techniques for getting the output you need.
There are good graphical file-comparison programs available. They aren’t always the best tool for comparing files, though. One reason to use the command-line diff is that it can give you much more control over how differences are found and displayed.
You’re probably familiar with diff(1). We’ll look at some details of how diff works, some lesser-known options, and other techniques for getting the output you need.
This is the first of a series about diffutils, an important package of tools.
Different diffs
You might be surprised to know that, given two files, diff may not always give the same set of differences. As an example, Table One shows two snippets of C code in files named 1.c and 2.c.
Table One: Two short files
File 1.c
File 2.c
a *= b;
c *= d;
b *= c;
b *= c;
c *= d;
a *= b;
GNU diff version 2.8.1 shows that the first two lines of 1.c were deleted, and that two new lines were added to 2.c:
$ diff 1.c 2.c 1,2d0 < a *= b; < b *= c; 3a2,3 > b *= c; > a *= b;
But you might prefer to think that the first and third lines were replaced, while the second line is the same. This would be a valid diff output, too:
$ hypothetical_diff 1.c 2.c 1c1 < a *= b; --- > c *= d; 3c3 < c *= d; --- > a *= b;
…
Please log in to view this content.
Not Yet a Member?
Register with LinuxMagazine.com and get free access to the entire archive, including: