A comparison between two similar files makes sense in order to know what differences exist between the two files. For example, comparing the results obtained by a command ran on two sets of data. Another example can be comparing an older and a newer version of a shell script file in order to know what modifications have been made in script. Shell provides the diff
command for file comparison.
The diff
command is used to compare files line by line. The syntax of using the diff
command is as follows:
diff [option] file1 file2
Where, file1
and file2
are the files to be compared.
The options of the
diff
command are explained in the following table:
Option |
Description |
---|---|
|
Only print if files differ |
|
Print a message on |
|
Display the |
|
Do case-insensitive comparison of the files' content |
|
Ignore changes in the number of whitespace |
|
Output |
|
Consider files as text files while comparison |
The diff
command shows the comparison results for the added, removed, and modified lines between two files.
We will consider the comparison_file1.txt
and comparison_file2.txt
text files as an example:
$ cat comparison_file1.txt # Viewing content of file
This is a comparison example.
This line should be removed.
We have added multiple consecutive blank spaces.
THIS line CONTAINS both CAPITAL and small letters
$ cat comparison_file2.txt # Viewing content of file
This is a comparison example.
We have added multiple consecutive blank spaces.
this line contains both CAPITAL and small letters
Addition of a line
Now, we will compare the comparison_file1.txt
and comparison_file2.txt
files:
$ diff comparison_file1.txt comparison_file2.txt
2,5c2,4
<
< This line should be removed.
< We have added multiple consecutive blank spaces.
< THIS line CONTAINS both CAPITAL and small letters
---
> We have added multiple consecutive blank spaces.
> this line contains both CAPITAL and small letters
> Addition of a line
Here, <
(less than) means removed lines and >
(greater than) means added lines.
Using the -u
option makes the diff
output even more readable as follows:
$ diff -u comparison_file1.txt comparison_file2.txt
--- comparison_file1.txt 2015-08-23 16:47:28.360766660 +0530
+++ comparison_file2.txt 2015-08-23 16:40:01.629441762 +0530
@@ -1,6 +1,5 @@
This is a comparison example.
-
-This line should be removed.
-We have added multiple consecutive blank spaces.
-THIS line CONTAINS both CAPITAL and small letters
+We have added multiple consecutive blank spaces.
+this line contains both CAPITAL and small letters
+Addition of a line
Here, '-
' tells the lines available in an older file (comparison_file1.txt
), but which is no longer present in the newer file (comparison_file2.txt
).
The '+
' tells lines being added in newer file (comparison_file2.txt
).
We can even do a case-insensitive comparison of the content using the –i
option:
$ diff -i comparison_file1.txt comparison_file2.txt
2,4c2
<
< This line should be removed.
< We have added multiple consecutive blank spaces.
---
> We have added multiple consecutive blank spaces.
5a4
> Addition of a line
To ignore multiple blank spaces, use diff
with make -b
option:
$ diff -bi comparison_file1.txt comparison_file2.txt
2,3d1
<
< This line should be removed.
5a4
> Addition of a line
3.12.74.189