diff command is used to compare two files line by line and contents of the directories. In this guide we will cover the diff command in Linux.
How to Use the diff Command
Following is the basic syntax of the
diff [OPTION]... FILES
You can get the output of
diff command in multiple formats like normal, context and unified formats. If the files match, it will not show any output.
You can save the output of command to a file using the redirection operator:
diff filename1 filename2 > patch
Here are two files which we will use in this guide to explain working of
Ubuntu Arch Linux Debian CentOS Fedora
Kubuntu Ubuntu Debian Arch Linux Centos Fedora
By default, when run the
diff command for two files, it will show output in the normal format:
diff file1 file2
It should show output something like below:
0a1 > Kubuntu 2d2 < Arch Linux 4c4,5 < CentOS --- > Arch Linux > Centos
The output of the normal format contains one or multiple sections which shows the differences.
change-command < from-file-line... --- > to-file-line...
In the output
4c4,5 are change commands. Each change command contains the following, from left to right:
- The line number or range of lines in the first file.
- A special change character.
- The line number or range of lines in the second file.
Following are the other change characters:
a– Add the lines.
c– Change the lines.
d– Delete the lines.
The change command is followed by the complete lines that are removed (
<) and added to the file (
Following is the explaination of the output:
0a1– It means add line
1of the second file at the starting of the
> Kubuntu– The line from the second line that is added to the first file as described above.
2d2– Meaning of this is to delete line
2in the first file. The
dsymbol means that if the line is not deleted it would appear on line
2in the second file.
< Arch Linux– the deleted line.
4c4,5– Change line
5in the first file with lines
4-5from the second file.
< CentOS– The line in the first file to be replace.
> Arch Linux and > CentOS– Lines from the second file replacing the line in the first file.
To show output in context format, use
-c option with
diff command. In the context format output, the
diff command shows few lines of context around the lines that displays differences.
diff -c file1 file2
*** file1.txt 2020-09-17 14:03:48.890297526 +0000 --- file2.txt 2020-09-17 14:04:08.330295458 +0000 *************** *** 1,6 **** Ubuntu - Arch Linux Debian ! CentOS Fedora --- 1,7 ---- + Kubuntu Ubuntu Debian ! Arch Linux ! Centos Fedora
In above output, you can see it starts with file names and the timestamps, after that it shows the differences. It should display each section as below:
to-file-line-numbers – The line numbers or comma-separated range of lines in the first and second file, respectively.
to-file-line – Shows the line which have differences:
- It shows line starting with two spaces which are same in both files.
- When line starting with
-symbol, that means that line is not in second file.
- Line beginning with
+symbol means that line is not in first file.
- Lines starting with the exclamation mark (
!) are the lines that are changed between two files.
Below is the explanation of the important parts of the output:
Here only one section have the difference.
*** 1,6 ****and
--- 1,7 ----shows the range of the lines from the first and second files that are included in this section.
- The lines are started with double space are same in both files, those are
Fedora, and the last empty line.
- The line with value Arch Linux of the first file shows that same value exists in second file but the positions are different.
+ Kubuntufrom the second file corresponds to nothing in the first file.
! CentOSfrom the first file and lines
! Arch Linuxand
! CentOSfrom the second file are changed between the files.
The context lines defaults to three, if you would like to change you can use the
diff -C 1 file1 file2
*** file1.txt 2020-09-17 14:03:48.890297526 +0000 --- file2.txt 2020-09-17 14:04:08.330295458 +0000 *************** *** 1,5 **** Ubuntu - Arch Linux Debian ! CentOS Fedora --- 1,6 ---- + Kubuntu Ubuntu Debian ! Arch Linux ! Centos Fedora
The unified output format contains the smaller output and it’s advance version of context format.
You should use -u option with diff command to output in unified format:
diff -u 1 file1 file2
--- file1.txt 2020-09-17 14:03:48.890297526 +0000 +++ file2.txt 2020-09-17 14:04:08.330295458 +0000 @@ -1,6 +1,7 @@ +Kubuntu Ubuntu -Arch Linux Debian -CentOS +Arch Linux +Centos Fedora
The diff command is most useful command to compare text files for differences in Linux systems. For more information, type
man diff in your terminal.
If you have any questions, please leave a comment below.
Leave a Reply