The comparison of files highlights the differences and similarities/differences between them. This process is useful for modifying the files by adding additional information, removing unnecessary data, and even searching and replacing the desired words. Linux offers the built-in command line to perform this task through the terminal as required.
This guide explains how to compare two files:
- Using the “diff” Command
- Using the “diff –color” Command
- Using the “vim” Text Editor
Sample Files to be Used in this Post
Two sample files are used in this post, the first line contains six lines in total and the overall content is:
$ cat -n SampleFile1.txt
The second file, named “SampleFile2.txt,” also contains six lines content:
$ cat -n SampleFile2.txt
Method 1: Use the “diff” Command to Compare Two Files
Linux supports the “diff(difference)” command utility for comparing two files line by line through the terminal. It also gives the suggestions such as adding, removing, and changing the content.
The “diff” command works on its generalized syntax that is stated here:
$ diff [OPTION]... FILES
The square brackets show the supported options of the “diff” command to perform special tasks.
Let’s use this syntax to compare the “SampleFile1” and the “SampleFile2” differently.
Example 1: Scan the Difference
The “diff” command highlights the difference between two files in the form of a standard label at the start of each line in this format:
Scanning Output Label Format
[Line no of file1, a(add),c(change),d(delete), Line no of file2].
Execute the “diff” command to scan the “SampleFile1.txt” and “SampleFile2.txt”:
$ diff SampleFile1.txt SampleFile2.txt
The output of the “diff” command highlights the difference such as:
- 1d0: Suggests the first line of SampleFile1.txt should be removed (d) otherwise it would appear at the “0” line of SampleFile2.txt.
- 3c2: Line 3 of the SampleFile1.txt should be changed(c) with the second line of SampleFile2.txt
- 4a4: Line 4 of SampleFile1 should be added into the fourth line of the SampleFile2.
The “diff” command also highlights the difference using different symbols followed by “-W(width, ignoring whitespaces)” “50” between both file’s content:
$ diff -y -W 50 SampleFile1.txt SampleFile2.txt
The output denotes the symbols that identify the differences are:
- <: Line has been removed from the “SampleFile2.txt” file.
- |: Line is different from the first “SampleFile1.txt”.
- >: Line is additional in the “SampleFile2.txt”.
Example 2: Print Dffenence in Context Mode
The “diff” command can display the difference between files in different formats, in which the “context-mode” is one of them.
Execute the “diff” command followed by the “-c” flag to get the output in context mode:
$ diff -c SampleFile1.txt SampleFile2.txt
The context mode uses the following symbols to highlight the difference:
- ***: denotes the name of the file i.e., “SampleFile1.txt” alongside its date and time when it is created.
- —: denotes the name and timestamp of the second file “SampleFile2.txt”.
- **********: acts as a separator.
- ****1,6****: Shows the range of SampleFile1, and the next one is for SampleFile2.
- -(delete): This shows the line exists in the first file but not in the second one. You should remove it from the first file.
- +(add): Line does not exist in the first file. Try to add it to the second file.
- !(change): Changes the line for matches
Example 3: Get Diffenece in Unified Format
The “unified” format is the enhanced or improved form of the “context” mode and displays a short output like this:
$ diff -u SampleFile1.txt SampleFile2.txt
The new character @@ shows the ranges of the lines of both compared files. The “+” requires the addition of lines, and the “-” needs the removal of lines.
Example 4: One Liner Difference
If the user wants to get the output in just one line showing whether the files are “different” or “identical,” use the “-q” flag:
$ diff -q SampleFile1.txt SampleFile2.txt
The output confirms that “SampleFile1” is different from “SampleFile2”
Method 2: Use the “diff –color” Command to Compare Two Files
The “diff” command supports the special “–color” flag that enables the highlighting feature to clearly show the difference. The colorized output enhances the readability and provides a better visualization of the difference and similarities.
Example 1: Highlight the Difference
The “diff –color” command highlights the difference between two files in RGB color format:
$ diff --color sampleFile1.txt SampleFile2.txt
For more examples and details of the “–color” flag of “diff” command, read our details guide on “How to Color diff Output?”.
Method 3: Use the “vim” Text Editor to Compare Two Files
Vimdiff is another command line tool to compare two files. It is similar to the “diff” command and can edit and show the difference between more than two files in the “vim” text editor. Its colorized output highlights the difference between compared files.
Specify the file names one by one with the “vimdiff” command for their comparison:
$ vimdiff SampleFile1.txt SampleFile2.txt
The output of the “vimdiff” command shows the difference in the vim editor.
Linux offers the “diff(difference)” command line utility to compare files through the terminal. It also supported a special “–color” flag to get the comparison output in colorized format. In addition, the user can also use the “vimdiff” command associated with the “vim” text editor to perform this task.
This guide has listed all possible methods to compare two files in a Linux terminal.