Linux provides you with multiple options to compare two files. The most popular command to compare two files is diff. In this tutorial we will look at diff command and some other options the you can use to compare two files in Linux.
Let’s create two files to use as examples throughout the tutorial.
Text for file 1:
Welcome to the best source for learning Linux. How are you doing?
Text for file 2:
Welcome to Linux For Devices! How are you doing?
Table of Contents
Diff command to Compare two files in Linux
Diff command is the most commonly used command when it comes to comparing two files. The name is short for “difference“. The output tells you the steps you need to follow in order to change the first file to make it match the second file.
Syntax for using diff command is:
diff [option] [file 1] [file 2]
Let’s see diff command in action.
diff [file 1] [file 2]
The output only displays the lines that don’t match. The lines are displayed in the order of files as mentioned in the command.
Let’s try and understand the output.
You can see that the output starts with ‘1c1’. This is in the format [range][abc][range].
Where the first range field belongs to first file and the second range field belongs to the second file.
The letter in the middle can be one of the following three.
- a: add
- c: change
- d: delete
In our example, ‘1c1’ means that you need change line number one of the first file to make sure that the two files sync up to line number one of the second file.
3d2 means that you need to delete line number three of the first file in order to make sure that the two files sync at line number two of the second file.
Context mode (diff -c)
To see the difference between two files in context mode use -c flag along with diff command.
diff -c [file 1] [file 2]
Let’s try and understand the output.
The output starts with meta information about the file. This is information like filename, date and time of creation and permissions.
The next line is *** 1,3 ****. This line contains the from and to line number being considered during comparison.
After this is the text from file 1. Here you can see certain symbols before the line. Let’s see what these symbols mean.
- + : The line is missing in the first file. You can either add it in the first file or delete it from the second file.
- – : The line is missing in the second file. You can either add it in the second file or delete it from the first file.
- ! : The line needs to be changed in order to make it match in both the lines.
The same information is repeated for the second file.
Unified format (diff -u)
The unified format is an improved version of context mode. The syntax is as follows :
diff -u [file 1] [file 2]
You can see that it conveys the same information but in a concise manner.
Vimdiff to Compare two files in Linux
Another way to compare two files is Vimdiff. This will work only if you have the Vim editor on your system.
The syntax for comparing two files using vimdiff is:
vimdiff [file 1] [file 2]
This is what the output looks like. It opens the two files in Vim editor side by side. It also highlights the parts that don’t match in the two files. You should use vimdiff is you are comfortable using Vim editor.
The Perl script colordiff is a wrapper for ‘diff’ and produces the same output but with pretty ‘syntax’ highlighting. It also gives you the option to customize the colours.
To install colordiff on your system use the apt command:
sudo apt install colordiff
This command will install colordiff on your system. To compare two files using colodiff use :
colordiff [file 1] [file 2]
You can see that the output is the same as the one produced above in diff command. The only difference is that of colour in the output.
This tutorial was about different ways you can use to compare files in Linux.