Introduction

This lesson will cover how to compare the contents of files. If you want to compare two files, and display the differences, you can use the diff , the sdiff or vimdiff command. The diff will display the differences between two files. While the sdiff command will display the differences with file1 on the left and file2 on the right. vimdiff will use the vim editor to display the differences between two files.

Command Meaning
diff file1 file2 Compare two files.
sdiff file1 file2 Side-by-side comparison. file1 on the left and file2 on the right.
vimdiff file1 file2 Highlight differences in vim

diff Output

Here is just an example of the first line of output produced by diff.

$ diff file1 file2
3c3
...

The first line is represented in lineNumber-character-lineNumber format.

  • The first number represents line numbers from the first file
  • The second number represents line numbers from the second file.

The middle character separating the line numbers will either be a

  • A - represents an Addition
  • C - represents a Change
  • D - represents a Delete

In above example, the third line of the first file is changed or different than the thrid line in the second file.

Full Example:

This is an example of an entire output from the diff command.

$ diff file1 file2
3c3
< this is a line in file1
---
> This is a line in file2

The output that follows the less than sign (<) belongs to the first file. The text following the greater than sign (>) belongs to the second file. The three dash (---) is a separator.

sdiff Output

In the sdiff output, the pipe or vertical bar character (|) means that the text differs in the files on that line. You may also see the less than sign (<), which means that the line only exists in the first file. The greater than sign (>) means that line only exists in the second file.

$ sdiff file1 file2
line in file1      |       line in file2
                   >       more in file2

vimdiff Output

When you run vimdiff, both files will be pulled up in separate windows. To switch between the windows, type Ctrl ww. To close the current file in the current window, type :q. If you want to close both files at once and quit, type :qa, and if you have made some changes that you don't want to save you can type :qa! to force quit all.

Type Meaning
Ctrl ww Go to next window.
:q Quit (close current window)
:qa Quit all (close both files)
:qa! Force quit all

Example

Suppose we have two files testOne.txt and testTwo.txt , shown as below:

$ cat -n testOne.txt
   1  Address Line 1:  45
   2  Address Line 2:  South Park
   3  City:  Oxford
$ cat -n testTwo.txt
   1  Address Line 1:  47
   2  Address Line 2:  South Park
   3  City:  Oxford

Let's use the diff command to look at the differences between those two files. The 1c1 means the first line of the first file is changed or different from the first line of the second file.

$ diff testOne.txt testTwo.txt
1c1
< Address Line 1: 45
---
> Address Line 1: 47

Let's use sdiff. You can see that it places the files side by side, and the vertical bar or the pipe symble (|) displays the line that has difference.

 
$ diff testOne.txt testTwo.txt
Address Line 1:  45                     | Address Line 1:  45
Address Line 2:  South Park               Address Line 2:  South Park
City:  Oxford                             City:  Oxford

Let's use vimdiff. You can see that vim is highlighting the differences between the files. You can use Ctrl ww to switch window. If you want to close one window, enter :q. And enter :qa to close them at the same time.

 
$ vimdiff testOne.txt testTwo.txt

vimdiff - Compare the contents of files