diff Command in Linux



The Linux diff command compares files line by line to identify any differences between them. It is a handy tool for creating patches, managing versions, and tracking changes in the files. However, it is also used to compare the contents of directories.

Table of Contents

Syntax for diff Command

The basic syntax for the Linux diff command is as follows −

diff [options] [file1] [file2]

The [options] signifies the place for various flags and options diff command offers. The [file1] and [file2] indicate the first and second filenames respectively. Note that when comparing directories, the [file1] and [file2] will be replaced by [directory1] and [durectory2] respectively.

Options for diff Command

The diff command comes with many options to get the desired output. Some commonly used options and flags are listed below −

Flags Options Description
-q --brief It reports only where there is a difference
-s --report-identical-files It reports only when the files are identical
-c / -C NUM --context=NUM It displays difference with a context of 3 lines around the change: context can also be modified
-u / -U NUM --unified=NUM It displays difference with a context of a few lines around the change: context can also be modified
-y --side-by-side It displays the output side-by-side in two columns
-t --expand-tabs It expands tabs with spaces
-r --recursive It recursively compares any subdirectories found
-x PAT --exclude=PAT It excludes the files that match the PATTERN while comparing directories
-X FILE --exclude-from=FILE It excludes the files that match the pattern given in a FILE while comparing directories
-S FILE --starting-file=FILE To start a comparison of files from a specific starting file in directories
-i --ignore-case It ignores the case differences
-E --ignore-tab-expansion It ignores the changes due to tabs
-B --ignore-blank-lines It ignores blank lines as a difference
--ignore-file-name-case It ignores cases when comparing the file names
-a --text It treats all files as text files
--color=WHEN To colorize the differences in the output, WHEN: auto, always, never
-d --minimal

It allows us to find the smallest set of differences

Note: It is slower than the normal algorithm

--label LABEL

To label the file or directory names

Note: It can be used multiple times

--help To display the brief help about command
-v --version To check the version

Understanding the diff Command Output

The diff command output is not straightforward, it has to be decoded to comprehend. The output contains special symbols that indicate various operations.

Let’s take an example of the following output −

Understanding the diff Command Output

In the output −

  • < points to the content file1
  • > points to the content file2
  • Symbol a indicates add
  • Symbol c indicates change
  • Symbol d indicates delete
  • Numbers before the symbol indicate the line numbers of the file1, while numbers after the symbol indicate the line numbers of the file2 e.g., 3d2, 5c4,6
  • The numbers separated by a comma (x,y) indicate the range of lines; for example, 4,6 indicates lines 4 to 6 (it can be either file file1 or file2)

Using diff Command in Linux

This section demonstrates the diff command usage in Linux through examples −

Comparing Two Files

The comparison of two files is one of the basic operations of the diff command.

Contents of two sample files file1.txt and file2.txt are given in the following image −

Comparing Two Files 1

To compare these two files, execute the following command −

diff file1.txt file2.txt
Comparing Two Files 2

In the above output −

  • 3d2 indicates that line 3 of file1 should be deleted (d) to match file2 starting from line 2.
  • < Linux Mint indicates line 3 of file1 needs to be deleted.
  • 5c4,5 indicates that line 5 of file1 should be replaced with lines 4 to 5 of file2 to match the content of both files.
  • < Debian shows the lines 5 of file1 while >Fedora >Manjaro shows lines 4 and 5 of file2 respectively.
Comparing Two Files 3

After making these changes in file1, the contents of both files will be matched.

Note − In the following examples, we will be using the same sample files.

Comparing Two Files in Context Format

The default diff command output can be formatted using various options. One of them is the context format. The context format by default displays the context of 3 lines around the change, however, using the -C or --context option we can change the number of context lines around the change.

diff -c file1.txt file2.txt
Comparing Two Files in Context Format 1

In the above output −

  • (*** file1.txt) shows the timestamps of file1
  • (--- file2.txt) shows the timestamps of file2
  • *** 1,5 *** shows the context lines of file1
  • --- 1,5 --- shows the context lines of file2
  • (-) indicates a line to be deleted
  • (!) indicates to change the lines in file1 with the lines in file2

Though not present in the example output the (+) sign indicates a line to be added.

To change the context lines −

diff -C 1 file1.txt file2.txt
Comparing Two Files in Context Format 2

The output shows only 1 line around the change.

Comparing Two Files in Unified Format

The unified format is compact and easier to understand. It essentially displays output with a few lines of context.

diff -u file1.txt file2.txt
Comparing Two Files in Unified Format

In the output −

  • --- and +++ shows the file1 and file2 respectively
  • @@ -1,5 +1,5 @@ indicates the range of lines of both files involving changes
  • (-) indicates to remove the line
  • (+) indicates to add the lines

In summary, deleting Linux Mint and Debian and adding Fedora and Manjaro make the contents of both files the same.

Comparing Two Files Side-by-Side

The diff command also generates a side-by-side comparison of both files.

diff -y file1.txt file2.txt
Comparing Two Files Side-by-Side

The left column shows the contents of file1, while the right column shows the contents of file2.

In the output −

  • < indicates a line to be deleted
  • | indicates a line to be modified
  • > Indicate a line to be added

Making the above changes in file1 will match the contents of both files.

Comparing files while Ignoring Case

To eliminate the case sensitivity from the file comparison the -i flag is used.

The contents of both files are shown in the following image after making a modification −

Comparing files while Ignoring Case

In file1 and file2 line 1 is different in terms of case.

The output without ignoring the case is as follows −

Comparing Files Without Ignoring Case

Let’s implement the ignore case flag, -i

diff -i file1.txt file2.txt
Comparing Files With Ignoring Case

The output shows that the command ignores line1 of both files as they differ in terms of the case.

Comparing Directories

The diff can be employed to compare the contents of the directories.

The contents of the directories dir1 and dir2 to be compared are shown in the following image −

Comparing Directories 1

To compare directories, the -r flag or --recursive option is used −

diff -r dir1 dir2
Comparing Directories 2

In the above output −

  • Only in dir1: dir3 indicates the subdirectory dir3 is only present in dir1 not dir2
  • Only in dir1: file1.txt indicates the file1.txt is only present in dir1
  • Only in dir2: file4.txt indicates the file4.txt is only present in dir2

So, from the above output, it is inferred the dir3, file1.txt, and file4.txt are different in both files and the remaining contents are the same.

To make a comparison more concise files or directories can be excluded using the -x flag with the file or directory name. For instance, to exclude file4.txt, use the command given below −

diff -r -x "file4.txt" dir1 dir2
Comparing Directories 3

Displaying Comparison with Colors

To make the comparison more prominent, it can be highlighted using the --color option. The example to enable colors in comparison is given below −

diff --color=always file1.txt file2.txt
Displaying Comparison with Colors

Other settings that can be assigned to the --color option are auto and never. The auto automatically enables the colors while never disables the colors.

Note that if no setting is explicitly assigned to --color, it defaults to auto.

Conclusion

The Linux diff command is a handy command line utility to compare two files or the contents of the directories. Comparing files is crucial for monitoring changes, generating patches, and version control. Moreover, it is also important for backing up the data.

In this tutorial, we covered the basics of the diff command in Linux and its usage using various options.

Advertisements