How To Compare Two Text Files Using Linux

Compare 2 Files Using Linux
Compare 2 Files Using Linux.

This guide will show you how to use Linux to compare two files and output their difference to the screen or to a file.

You do not need to install any special software in order to compare files using Linux but you do need to know how to open a terminal window.

As the linked guide shows there are many ways to open a terminal window using Linux. The simplest is to press the CTRL, ALT and T keys at the same time.

Creating The Files to Compare

In order to follow along with this guide create a file called "file1" and enter the following text:

10 green bottles standing on a wall

10 green bottles standing on a wall

If one green bottle should accidentally fall

There would be 9 green bottles standing on the wall

You can create a file by following these instructions:

  1. Open the file by typing the following command: nano file1
  2. Type the text into the nano editor
  3. Press CTRL and O to save the file
  4. Press CTRL and X to exit the file

Now create another file called "file2" and enter the following text:

10 green bottles standing on a wall

If 1 green bottle should accidentally fall

There'd be 9 green bottles standing on the wall

You can create a file by following these instructions:

  1. Open the file by typing the following command: nano file2
  2. Type the text into the nano editor
  3. Press CTRL and O to save the file
  4. Press CTRL and X to exit the file

How to Compare Two Files Using Linux

The command used within Linux to show the differences between 2 files is called the diff command.

The simplest form of the diff command is as follows:

diff file1 file2

If the files are the same then there will be no output when using this command, however, as there are differences you will see output similar to the following:

2,4c2,3

< 10 green bottles standing on the wall

< If one green bottle should accidentally fall

< There would be 9 green bottles standing on the wall

...

> If 1 green bottle should accidentally fall

> There'd be 9 green bottles standing on the wall

Initially, the output may seem confusing but once you understand the terminology it is fairly logical.

Using your own eyes you can see that the differences between the 2 files are as follows:

  • The second file only has three lines whereas the first file has 4. 
  • The second file says "1 green bottle" on the third line whereas the first file says "one green bottle"
  • The second file says "there'd" instead of "there would" on the final line

The output from the diff command shows that between lines 2 and 4 of the first file and lines 2 and 3 of the second file there are differences.

It then lists the lines from 2 to 4 from the first file followed by the 2 different lines in the second file.

How to Just Show If the Files Are Different

If you just want to know if the files are different and you aren't interested in which lines are different you can run the following command:

diff -q file1 file2

If the files are different the following will be displayed:

Files file1 and file2 differ

If the files are the same then nothing is displayed.

How to Show a Message If the Files Are the Same

When you run a command you want to know that it has worked correctly, so you want a message to be displayed when you run the diff command regardless as to whether the files are the same or different

In order to achieve this requirement using the diff command, you can use the following command:.

diff -s file1 file2

Now if the files are the same you will receive the following message:

Files file1 and file2 are identical

How to Produce the Differences Side by Side

If there are lots of differences then it can very quickly become confusing as to what the differences actually are between the two files.

You can change the output of the diff command so that the results are shown side by side. In order to do this run the following command:

diff -y file1 file2

The output for the file uses the | symbol to show a difference between the two lines, a < to show a line that has been removed and a > to show a line that has been appended.

Interestingly if you run the command using our demonstration files then all the lines will show as different except for the last line of file 2 which will be shown as having been deleted.

Restricting the Column Widths

When comparing two files side by side it can be hard to read if the files have lots of columns of text.

To restrict a number of columns use the following command:

diff --width=5 file file2

How to Ignore Case Differences When Comparing Files

If you want to compare two files but you don't care whether the case of the letters is the same between the two files, then you can use the following command:

diff -i file1 file2

How to Ignore Trailing White Space at the End of a Line

If when comparing the files you notice loads of differences and the differences are caused by white space at the end of the lines you can omit these as showing up as changes by running the following command:

diff -Z file1 file2

How to Ignore All White Space Differences Between Two Files

If you are only interested in the text in a file and you don't care whether there are more spaces in one than the other you can use the following command:

diff -w file1 file2

How to Ignore Blank Lines When Comparing Two Files

If you don't care that one file may have extra blank lines in it then you can compare the files using the following command:

diff -B file1 file2

Summary

You can find more information by reading the manual for the diff command.

man diff

The diff command can be used in its simplest form to just show you the differences between 2 files but you can also use it to create a diff file as part of a patching strategy as shown in this guide to the Linux patch command

Another command you can use to compare files is the cmp command as shown by this guide. This compares files byte by byte.