Definition of Linux Diff Command
Diff command in Linux helps in comparing the data between two files line by line and when any difference is found between the files then the differences will also be displayed along with the line numbers. Diff command also helps in comparing the data between two directories.
The abbreviation of diff is different. It is to be noted that diff command in Linux uses a few special symbols and instructions which are needed in making two files similar.
Special symbols include:
- a: this represents that context has been added.
- c: this represents that context has been changed.
- d: this represents that context has been deleted.
Syntax:
The basic syntax of Diff Command is given below:
diff [options] file1 file2
The result of the diff command in Linux can be in the below format:
- It can be in a normal format
- It can also be a context
- It can also be in a unified format.
How Diff Command works in Linux?
Below are the options available in diff command in Linux. Options with their description are also mentioned below for better understanding. –help command will give you all the options that are available for diff command in Linux.
Options | Description |
–normal | The output displayed is a normal difference (the one that is displayed by default) |
-q, –brief | It reports only if the files differ |
-s, –report-identical-files | It reports if two files are the same and have no differences between them |
-y, –side-by-side | The output is displayed in two-column format |
-t, –expand-tabs | It will help in expanding the tabs to spaces in the output format. |
-r, –recursive | It will recursively help in comparing any subdirectories that are found |
-i, –ignore-case | This option is used in ignoring any case diff in file data. |
-a, –text | This option will be treating all the files as text |
–help | Prints the options that are available and will exit |
-v, –version | output version information and exit |
Examples of Linux Diff Command
Let us consider there are two files test1.txt and test2.txt. Now we will compare them by using diff command.
cat test1.txt
cat test2.txt
-
No Option:
Now when we use the diff command for the above two files and when there is no result showed that means there is no difference between the above two files.
Syntax: diff file1 file2
Example: diff test1.txt test2.txt
When there is a difference in both the files and when we use the diff command, the output will be displayed as shown below:
cat test1.txt
cat test2.txt
4c4 means that 4th line in test1.txt should be changed to make the two files(test1.txt and test2.txt) the same.
diff test1.txt test2.txt
-
Option -c:
When we use option ‘c’ in diff command, the result will be displayed in context format.
Syntax: diff -c file1 file2
Example: cat test1.txt
diff -c test1.txt test2.txt
Here in the output, the first two lines will give you the information of file names along with the modification dates of the two files. The first file will be represented with three stars (***) and the second file will be represented with three hyphens(—).
When the output has a single hyphen “-” that means the line will need to be removed and plus symbol “+” means the line would need to be added to the file. When any file that does not need any modification then it is expected to be prefixed with two spaces.
-
Option -u:
When we use the option ‘-u’ in diff command, the result will be displayed in a unified format.
Syntax: diff -u file1 file2
Example: diff -u test1.txt test2.txt
Here the output is more same with context format (option -c) but in option -u (unified format) the output will be displayed in a concise way, in the output the first 2 lines will represent that the file name with its changed time and date.
The first file will indicate as three hyphens (“—“) and the second file will indicate with three-plus symbol in the output(“+++”). The symbol ‘-lemon’ will represent that the line would need to be added to the first file and the symbol ‘+orange’ will represent that the line would need to be removed from the first file to make it similar to the second file.
-
Option -i:
Generally, diff command in Linux is case sensitive. If we would like to ignore any case sensitive in diff command, then we can use option -i in the argument as shown below.
Syntax: diff -i file1 file2
Example: cat test1.txt
cat test2.txt
diff test1.txt test2.txt
Here in the above screenshot, we can see that when there is no option mentioned in the first example, then the output displayed has shown the difference without ignoring the case sensitive data.
So if we would need to ignore the case sensitive text, then we will need to use option -i as shown in the above screenshot.
-
Option -s:
In few situations, we would need to display the message if both the files are similar. So we can use option -s to display the message as shown below.
Syntax: diff -s file1 file2
Example: diff -s test1.txt test2.txt
-
Option -b:
There are situations when two files are identical but there would a white space difference in the two files. To ignore the white space difference, we can use option -b as shown below.
Syntax: diff -b file1 file2
Example: cat test1.txt test2.txt
Here in the below screenshot, we can see that when no option is mentioned in the diff command, then
the white space difference is mentioned in the output.
diff test1.txt test2.txt
However, if we would like to ignore the white spaces in the two files, we can use the -b option. We can see in the below screenshot that by using -b option in the diff command, the output has ignored the whitespaces.
diff -b test1.txt test2.txt
There are some situations where there might be one or more white space differences in between the two files. To ignore all the white space differences, we can use option -w along with diff command in Linux.
diff -w test1.txt test2.txt
Conclusion
Diff command in Linux helps in comparing the data between two files line by line and when any difference is found between the files then the differences will also be displayed along with the line numbers. Diff command also helps in comparing the data between two directories. It is to be noted that diff command in Linux uses a few special symbols and instructions which are needed in making two files similar.
Diff command is a very useful command in comparing two files line by line. They are generally used inline comparisons to identify the difference between them. The above article on diff command in Linux will give you a clear understanding of how to use the diff command in Linux.
Recommended Articles
This is a guide to Linux Diff Command. Here we also discuss the definition and how does Linux Diff Command works along with different examples and its code implementation. You may also have a look at the following articles to learn more –
16 Online Courses | 3 Hands-on Projects | 160+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses