1. Computing

Discuss in my forum

Example Uses of the diff Command

A Brief Introduction

By

The diff command is used to compare two files. The assumption is that the two files are mostly identical, otherwise the program output may not be very useful. The diff command will then point out any lines where the two files differ. The method used to report the differences is based on the viewpoint that the first file is converted the second file. That is, it reports the sequence of instructions that would transform the first file to the second file.

To illustrate, consider the following two files. File 1 (file1.txt) contains:


“To-morrow, and to-morrow, and to-morrow,
Creeps in this petty pace from day to day,
To the last syllable of recorded time;
And all our yesterdays have lighted fools
The way to dusty death. Out, out, brief candle!
Life's but a walking shadow, a poor player,
That struts and frets his hour upon the stage,
And then is heard no more. It is a tale
Told by an idiot, full of sound and fury,
Signifying nothing.” 
― William Shakespeare
File 2 (file2.txt) contains:

“To-morrow, and to-morrow, and to-morrow,
Creeps in this petty pace from day to day,
To the last syllable of recorded time;
And all our yesterdays have lighted fools
The way to dusty death. Out, out, brief candle!
Life is but a walking shadow, a poor player,
That struts and frets his hour upon the stage,
Told by an idiot, full of sound and fury,
Signifying nothing.” 
― William Shakespeare
The diff command line that would compare these two files is:

diff file1.txt file2.txt
The output of this command would look something like this:

6c6
< Life's but a walking shadow, a poor player,
---
> Life is but a walking shadow, a poor player,
8d7
< And then is heard no more. It is a tale
The code "6c6" stands for "change line 6 in File 1 so that it matches line 6 in File 2. That is, replace line 6 File 1 with line 6 in File 2. The content of these lines is listed as well. First the content of line 6 of File 1 is printed out, then, separated by a line with three dashes, the content of line 6 of File 2 is printed out. The "less than" symbol at the beginning of the line means "remove" this line. The "greater than" symbol means "add" this line.

The code "8d7" stands for "delete line 8 in File1". The "7" refers to the line File 2 that is aligned with line in File 1 after which the deletion occurs.

If the order of the filename arguments to diff is reversed, the editing operations are reversed accordingly. That is, instead of a line being deleted, the line is added, etc.

Using the files from the example above and reversing the order of the arguments:


diff file2.txt file1.txt
we would get the following output:

6c6
< Life is but a walking shadow, a poor player,
---
> Life's but a walking shadow, a poor player,
7a8
> And then is heard no more. It is a tale
The code "7a8" stands for "insert line 8 from File 2 after line 7 in File 1".

If you want to compare three files instead of two you can use the diff3 command, which takes as input three filenames and prints out descriptions of any differences between them. Again, the assumption is that the three files are mostly identical.

You can use the cat or pr (print) commands to display the contents of a text file in the terminal (console) window.

  1. About.com
  2. Computing
  3. Linux
  4. Linux / Shell Commands
  5. Example Uses of the diff Command

©2014 About.com. All rights reserved.