Last Updated : 12 Jul, 2024
In the world of Linux, managing and comparing files is a common task for system administrators and developers alike. The ability to compare files line by line is crucial for identifying differences, debugging code, and ensuring the integrity of data. One powerful tool that facilitates this process is the diff
command. In this article, we will explore how to use the diff
command to compare files line by line in Linux.
diff stands for difference .The diff
command is a versatile utility that is pre-installed on most Linux distributions. Its primary purpose is to compare the contents of two files and display the differences between them. The command provides a comprehensive way to highlight changes, additions, and deletions in a clear and readable format.
This command is used to display the differences in the files by comparing the files line by line. Unlike its fellow members, cmp and comm, it tells us which lines in one file have is to be changed to make the two files identical.
The important thing to remember is that diff uses certain special symbols and instructions that are required to make two files identical. It tells you the instructions on how to change the first file to make it match the second file.
Basic Syntax of diff CommandThe basic syntax of the diff
command is as follows:
diff [OPTION]... FILE1 FILE2
Here, `FILE1`
and `FILE2`
are the two files you want to compare.
The `OPTION`
flag allows you to customize the behavior of the `diff`
command.
Option
Description
-c
or --context
Output differences in context mode
-u
or --unified
Output differences in unified mode (more concise)
-i
or --ignore-case
Perform a case-insensitive comparison
--ignore-all-space
Ignore whitespace when comparing lines
--brief
Output only whether files differ, no details
--recursive
Recursively compare directories
-y
or --side-by-side
Display the output in a side-by-side format
Pratical Implementaion of How to compare files line by line in Linux? : Comparing Two FilesCompare files line by line in Linux.
Lets say we have two files with names a.txt and b.txt containing 5 Indian states.
cat a.txtdisplaying content of files using cat command
cat b.txt
Now, applying diff command without any option we get the following output:
diff a.txt b.txtcomparing file line by line in linux
Let's take a look at what this output means. The first line of the diff output will contain:
Like in our case, 0a1 which means after lines 0(at the very beginning of file) you have to add Tamil Nadu to match the second file line number 1. It then tells us what those lines are in each file preceded by the symbol:
As a summary to make both the files identical, first add Tamil Nadu in the first file at very beginning to match line 1 of second file after that change line 2 and 3 of first file i.e. Uttar Pradesh and Kolkata with line 3 of second file i.e. Andhra Pradesh. After that change line 5 of first file i.e. Jammu and Kashmir with line 5 of second file i.e. Uttar Pradesh.
Deleting a Line in Files using diff CommandConsider the scenario where diff
indicates the need to delete a line. Given two files, a.txt
and b.txt
:
diff a.txt b.txtDeleting a Line in File
Here above output 3d2 means delete line 3rd of first file i.e. Telangana so that both the files sync up at line 2.
Viewing Differences in Context ModeTo view differences in context mode, use the -c option. Lets try to understand this with example, we have two files file1.txt and file2.txt:
displaying content of files using cat commanddiff -c file1.txt file2.txtViewing difference in context mode
In the above output:
***`
, and the second file is denoted by `---`
.***************`
serves as a separator.***`
are followed by a line range from the first file (lines 1 through 4). Four asterisks `****`
come next. The contents of the first file are then displayed with specific indicators:
`+`
: A line in the second file to be added to the first file for identical results.`-`
: A line in the first file to be deleted for identical results.---`
are followed by a line range from the second file (lines 1 through 4), separated by a comma. Four dashes `----`
follow, and the contents of the second file are displayed.To view differences in unified mode, use the -u option. It is similar to context mode but it doesn't display any redundant information or it shows the information in concise form.
displaying content of files using cat commanddiff -u file1.txt file2.txtViewing difference in Unified mode
In the above output:
---`
, and the second file is indicated by `+++`
.@@ -1
`,`
4 +1`
,`
4 @@`
denotes the line range for both files. In this case, it represents lines 1 through 4 in both files.-
.+
.In this example, the output indicates that to make both files identical, the lines containing "mv" and "comm" need to be deleted from the first file (file1.txt
), and the lines containing "diff" and "comm" need to be added to it.
By default, `diff`
is case-sensitive. To perform a case-insensitive comparison, use the `-i`
option:
The diff
command is then used to compare these files with the -i
option, which makes the comparison case-insensitive.
diff -i file1.txt file2.txtcase-insensitive Comparing
2d1
: This indicates a change in line 2 of the first file (file1.txt
). The d
stands for delete, and it says to delete line 2 from the first file.
< mv
: This line signifies the content of the line to be deleted. In this case, it is "mv."3a3
: This indicates an addition in line 3 of the first file (file1.txt
). The a
stands for add, and it says to add a line at position 3.
> diff
: This line represents the content to be added. In this case, it is "diff."In summary, the output tells us that to make both files identical (ignoring case), we need to delete the line containing "mv" from the first file (file1.txt
) and add the line "diff" at the same position. The diff
command, with the -i
option, allows for a case-insensitive comparison, making it consider "mv" and "MV" as the same during the analysis.
diff
Version
To check the version of `diff`
installed on your system, use the `--version`
option:
diff --versionDisplaying version of diff command
This command provides information about the version, licensing, and authors of the `diff`
utility.
In the Linux world, comparing files is a common task for system administrators and developers. The `diff
command is a handy tool that helps in this process. This article explores how to use `diff`
to compare files line by line in Linux. It covers the basic syntax, important options like context mode and unified mode, and practical applications such as creating patch files and recursively comparing directories. Whether you're debugging code or ensuring file integrity, understanding and mastering the `diff`
command is essential for efficient file management in Linux.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4