Site icon DesignLinux

Diff Command in Linux

Diff Command in Linux

The 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 command:

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 diff command.

Ubuntu
Arch Linux
Debian
CentOS
Fedora
Kubuntu
Ubuntu
Debian
Arch Linux
Centos
Fedora

Normal Format

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 0a1, 2d2 and 4c4,5 are change commands. Each change command contains the following, from left to right:

Following are the other change characters:

The change command is followed by the complete lines that are removed (<) and added to the file (>).

Following is the explaination of the output:

Context Format

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:

from-file-line-numbers and to-file-line-numbers – The line numbers or comma-separated range of lines in the first and second file, respectively.

from-file-line and to-file-line – Shows the line which have differences:

Below is the explanation of the important parts of the output:

Here only one section have the difference.

The context lines defaults to three, if you would like to change you can use the -C (--contexts) option:

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

Unified Format

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

Conclusion

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.

Exit mobile version