Sign In
Sign In

How to Use the diff Command in Linux

How to Use the diff Command in Linux
Awais Khan
Technical writer
Linux
17.10.2024
Reading time: 7 min

The diff command in Linux is a powerful tool that allows users to compare files and directories. With the help of this command, one can identify differences between files, and perform tasks like code reviews, configuration management, and version control. 

This tutorial will guide users through what is the diff command, its possible methods, and practical examples.

Introduction

The diff command is used in Linux to compare the content of two files line by line. When executed, this command analyzes the two files and outputs the differences in a specific format. The output shows which lines need to be added, deleted, or changed to make the files identical.

Basic Syntax and Options for diff

The basic syntax for the Linux diff command is provided below:

diff [options] file1 file2

Here,

  • diff is the command itself.

  • [options] are optional flags used to modify the behavior of the diff Linux command.

  • file1 and file2 are the two files used for Linux file comparison.

The following table describes a few options that can be used with diff:

Option

Description

-a

Process every file as a text file and perform a line-by-line comparison.

-b

Does not consider white space differences.

-c

Show differences with a few lines of context around them.

-d

Opt for a different algorithm to pinpoint a more concise set of changes.

-e

Output an ed script.

-E

Ignore changes due to tab expansion.

--binary

Compare files in binary mode.

-i

Ignore case differences in file contents.

-l

Paginate the output through pr.

-N

Treat absent files as empty.

-q

Report only when files differ.

-s

Report when files are identical.

-u

Display output in a unified format, showing differences more compactly.

-w

Ignore all white space.

For more details and to explore more options, the users can get help by opening the diff manual using the following command:

man diff

Comparing Two Text Files Using diff

There are two ways to compare files on Linux with diff.

Basic Comparison of Two Text Files

The basic way to use the diff in Linux is to compare two files line by line and display their differences. To compare two text files, file1.txt and file2.txt, one can use the following command:

diff file1.txt file2.txt

This command will output the differences between file1.txt and file2.txt.

Image1

Display Differences in a Unified Format

For a more readable format, the -u option can be used with diff. This option provides a unified format that includes a few lines of context around the differences. This makes it easier to understand the changes. Follow the command provided below:

diff -u file1.txt file2.txt

Image3

The unified format output includes line numbers, context lines, and change indicators. Lines starting with - indicate deletions, lines starting with + indicate additions and lines starting with a space are unchanged context lines.

Using diff for Directory Comparisons

The Linux command diff can also be used to compare directories, it can be done using the -r option. For example:

diff -r dir1 dir2

The above command when executed will recursively compare all files and subdirectories within dir1 and dir2.

Image2

Understanding diff Output and Symbols

The diff output uses specific symbols to indicate changes, these are provided below:

  • ---: Denotes the first file.

  • +++: Denotes the second file.

  •  @@ -1,4 +1,4 @@: This line is part of the unified diff format. It gives context about where the changes are happening in the files. @@ indicates the start of a change hunk. -1,4 means the chunk starts at line 1 in the first file and spans 4 lines. +1,4 means the chunk starts at line 1 in the second file and spans 4 lines.

  • <: This marker signifies lines that exist in the first file but not in the second one. Such lines must be removed from the first file to match the second file exactly.

  • >: This marker indicates lines that are in the second file but not in the first one. These lines should be added to the first file to make it identical to the second file.

  • -: This marker shows lines that have been deleted from the first file.

  • +: This marker indicates lines that have been inserted into the second file.

Let’s look at an example to make it clearer.

Suppose there are two files, file1.txt and file2.txt.

Contents of file1.txt:

apple
banana
cherry
date

Contents of file2.txt:

apple
banana
date
raspberry

Running the command diff file1.txt file2.txt will produce the following output:

Image1

Here’s how to interpret this output:

  • 3d2: This means that line 3 in file1.txt (cherry) needs to be deleted to match file2.txt. The d stands for "delete".

  • < cherry: This indicates that cherry is present in file1.txt but not in file2.txt.

  • 4a4: This means that after line 4 in file1.txt, users need to add "raspberry" to match file2.txt. The a stands for "add".

  • > raspberry: This indicates that raspberry is present in file2.txt but not in file1.txt.

Creating Patch Files with diff

To create a patch file, the -u (unified) option is used, which provides a more readable format by showing a few lines of context around the changes. The output is then redirected to a file, typically with a .patch extension. For example:

diff -u file1.txt file2.txt > changes.patch
  • diff -u: Compares file1.txt and file2.txt and generates a unified diff.

  • >: Redirects the output to a file named changes.patch.

Image4

To apply the patch, use the patch command like this:

patch file1.txt < changes.patch

Image7

Using diff with Various Output Formats

The diff also supports multiple output formats, here are a few examples.

Unified Format

This format gives users a snapshot of the changes with a few lines of context before and after each change. It’s great for quickly seeing what was added or removed.

diff -u file1.txt file2.txt

Image6

Context Format

This format shows more surrounding lines for each change and gives users a bigger picture of where the changes happened.

diff -c file1.txt file2.txt

Image9

Side-by-Side Format

This format places the two files next to each other and makes it easy to compare them line by line.

diff -y file1.txt file2.txt

Image8

Brief Format

This format gives a summary of whether the files differ but does not show the actual changes.

diff -q file1.txt file2.txt

Image11

Practical Examples of Using diff

Here are some practical examples of using the diff command in Linux.

Ignoring Case Differences

When comparing files, sometimes the case of the letters might differ, but the content is essentially the same. The -i option is used to ignore case differences. For example:

diff -i file3.txt file4.txt

In this example, diff will treat "Hello" and "hello" as identical, ignoring the case difference.

Image10

Ignoring White Space

White space differences, such as extra spaces or tabs, can be ignored using the -w option. This is useful when formatting changes have been made but the content remains the same. For example:

diff -w file1.txt file2.txt

Here, diff will ignore all white spaces, treating "Hello   World" and "Hello World" as identical.

Comparing Binary Files

The diff in Linux can also be used to compare binary files using the --binary option. This is helpful when users need to check if two binary files are identical or not. For example:

diff --binary file1.bin file2.bin

In this case, diff will compare the binary data of file1.bin and file2.bin and report any differences.

Ignoring Blank Lines

To ignore blank lines when comparing files, simply use the -B option, which is useful when blank lines have been added or removed.

diff -B file1.txt file2.txt

Conclusion

The diff is a versatile command in Linux for comparing files and directories. By understanding its syntax, options, and output formats, users can efficiently identify differences and manage changes. Whether for code reviews, configuration management, or version control, the diff command is an essential part of any Linux user’s toolkit.

Linux
17.10.2024
Reading time: 7 min

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start
Email us
Hostman's Support