Skip to content

Diff Command in Linux with Example

The Diff command is a powerful tool in Linux that allows users to compare and contrast two files or directories.

It is particularly useful for developers who need to keep track of changes made to their codebase, as well as for system administrators who need to monitor system logs and configuration files.

In this article, we will explore the various features of the Diff command and provide practical examples to help users get started.


The Diff command is a built-in utility in Linux that compares the contents of two files or directories and identifies the differences between them.

By default, Diff displays the differing lines between two files, along with their line numbers. Users can also use various options to customize the output, such as ignoring white space, ignoring case sensitivity, and displaying context lines.

In addition, the Diff command can be used to create patches that can be applied to other files, making it a valuable tool for version control.


Key Takeaways

  • The Diff command is a powerful tool in Linux that allows users to compare and contrast two files or directories.
  • The Diff command can be used to create patches that can be applied to other files, making it a valuable tool for version control.
  • Users can use the basic syntax “diff file1 file2” to compare two files, or “diff -r dir1 dir2” to compare two directories.

Understanding the Diff Command

Basic Concepts and Syntax

The diff command is a powerful tool in Linux used to compare two files or directories and display the differences between them. The syntax of the diff command is as follows:

diff [options] file1 file2

Here, file1 and file2 are the names of the files to be compared. The diff command compares the contents of these two files line by line and displays the differences between them.

Common Use Cases

The diff command has several options that can be used to customize its behavior. Some of the most commonly used options are:

  • -u: This option displays the differences between the two files in a unified format, which makes it easier to read and understand.

  • -r: This option is used to compare two directories and display the differences between them.

  • -c: This option displays the differences between the two files in a context format, which shows the lines before and after the differences.

  • -q: This option is used to display only whether the files are different or not, without showing the actual differences.

  • -i: This option ignores the case of the letters when comparing the files.

The output of the diff command includes information about the lines that are different between the two files. The lines that are different are displayed with a > or < symbol in front of them, indicating whether they are present in the first or second file.

Overall, the diff command is a useful tool for comparing files and directories in Linux. By using the various options available, users can customize the output of the command to suit their needs.

Comparing Files Line by Line

When comparing two files using the diff command, it is often useful to see the differences between the two files line by line. This can be achieved by using the -u or --unified option with diff.

Reading Diff Output

The output of diff when comparing files line by line can be a bit confusing at first, but it is actually quite straightforward. Each line in the output represents a line in one of the files being compared. Lines that are the same in both files are not shown in the output, while lines that are different are shown with a symbol indicating the type of change that was made.

Symbols and Their Meanings

The symbols used in the output of diff when comparing files line by line are as follows:

  • -: Indicates a line that was deleted from the first file.
  • +: Indicates a line that was added to the second file.
  • (space): Indicates a line that is the same in both files.
  • !: Indicates a line that was changed in both files.

In addition to these symbols, diff also includes line numbers for each line in the output. These line numbers can be used to quickly locate the changes in the files.

Here is an example of the output of diff when comparing two files line by line using the -u option:

--- file1.txt   2024-01-09 12:00:00.000000000 -0800
+++ file2.txt   2024-01-09 12:00:00.000000000 -0800
@@ -1,3 +1,3 @@
 This is line 1.
-This is line 2 in file1.
+This is line 2 in file2.
 This is line 3.

In this example, the --- and +++ lines indicate the filenames of the files being compared. The @@ line indicates the range of lines being compared, with the first number indicating the line number in the first file and the second number indicating the line number in the second file. The - and + symbols indicate the changes made to the files, with the line numbers indicating the line number in the respective files.

Overall, comparing files line by line using the diff command is a powerful tool for identifying changes between two files in a clear and concise manner.

Advanced Diff Options

Diff command in Linux offers several advanced options that allow users to customize the comparison process according to their needs. In this section, we will discuss some of the most commonly used advanced options of the diff command.

Context and Unified Formats

The context and unified formats are two different output formats that the diff command can produce. The context format shows the lines that are different between the two files along with some context lines that show the surrounding lines of the differences. The unified format is similar to the context format, but it provides more compact output by showing the differences in a single block.

To use the context format, users can use the -c option followed by the number of context lines to show. For example, to show three context lines, the command would be:

$ diff -c3 file1.txt file2.txt

To use the unified format, users can use the -u option followed by the number of context lines to show. For example, to show two context lines, the command would be:

$ diff -u2 file1.txt file2.txt

Recursive Comparison

The diff command can also compare directories recursively. This means that it compares the files in the directories and subdirectories and shows the differences. To use the recursive comparison, users can use the -r option. For example, to compare two directories named dir1 and dir2, the command would be:

$ diff -r dir1 dir2

Ignoring Case and White Space

Sometimes, users may want to ignore the case or white space differences while comparing files. The diff command provides options to ignore case and white space. To ignore case, users can use the -i option. For example, to compare two files ignoring case, the command would be:

$ diff -i file1.txt file2.txt

To ignore white space, users can use the -b option. For example, to compare two files ignoring white space, the command would be:

$ diff -b file1.txt file2.txt

Overall, the advanced options of the diff command provide users with the flexibility to customize the comparison process according to their needs. By using these options, users can make the diff command more powerful and efficient.

Working with Directories

Directory Comparison

The diff command can be used to compare directories as well. When comparing directories, diff will compare the files in each directory and report any differences it finds. To compare two directories, simply specify the paths to the directories as arguments to the diff command.

For example, to compare the contents of the directories /home/user/dir1 and /home/user/dir2, the following command can be used:

$ diff /home/user/dir1 /home/user/dir2

This will show a list of files that are different between the two directories. By default, diff will compare each file’s contents and report any differences it finds. However, it will not compare the contents of subdirectories unless the -r option is specified.

Handling Subdirectories

To compare the contents of subdirectories as well, the -r option can be used. This will cause diff to recursively compare the contents of all subdirectories within the specified directories.

For example, to compare the contents of the directories /home/user/dir1 and /home/user/dir2, including all subdirectories, the following command can be used:

$ diff -r /home/user/dir1 /home/user/dir2

This will show a list of files that are different between the two directories, including any files in subdirectories.

When comparing directories recursively, it can be useful to use the -q option to only report whether the files in each directory are identical or different, without showing the actual differences. This can make it easier to quickly identify which files are different without having to examine each file’s contents.

In summary, the diff command is a powerful tool for comparing the contents of directories in Linux. By default, diff will compare the contents of each file in the specified directories and report any differences it finds. To compare the contents of subdirectories as well, the -r option can be used. The -q option can be used to only report whether files are identical or different, without showing the actual differences.

Practical Examples and Tips

Creating and Applying Patches

One of the most common use cases for the diff command is to create and apply patches. A patch is a file that contains the differences between two files, and can be used to apply changes from one file to another. This is useful for distributing updates or changes to software or configuration files.

To create a patch, use the diff command with the -u option, which produces a unified diff format that includes context lines. For example, to create a patch for the changes made to a file called file1.txt, run:

$ diff -u file1.txt file1_modified.txt > patchfile.patch

This will create a patch file called patchfile.patch that contains the differences between file1.txt and file1_modified.txt. To apply the patch, use the patch command:

$ patch file1.txt < patchfile.patch

This will apply the changes in patchfile.patch to file1.txt. The patch command will automatically detect and apply the changes, and will also create a backup of the original file with a .orig extension.

Comparing Large Files Efficiently

When comparing large files, the diff command can be slow and memory-intensive. To compare large files efficiently, use the cmp command instead. The cmp command compares two files byte-by-byte and stops as soon as it finds a difference, making it much faster than diff for large files.

For example, to compare two large files called file1.bin and file2.bin, run:

$ cmp file1.bin file2.bin

If the files are identical, cmp will output nothing and exit with a status of 0. If the files are different, cmp will output the byte offset of the first difference and exit with a non-zero status.

When comparing large files, it can also be useful to redirect the output of cmp to a file for later analysis. For example, to compare two large files and save the output to a file called diff.txt, run:

$ cmp file1.bin file2.bin > diff.txt

Other commands

Frequently Asked Questions

How can I use the diff command to compare two files in Linux?

To compare two files in Linux using the diff command, simply type “diff” followed by the names of the two files you want to compare. For example: diff file1.txt file2.txt.

What is the syntax for displaying only the differences between files using diff?

To display only the differences between two files using diff, use the “-u” option followed by the names of the two files. For example: diff -u file1.txt file2.txt.

Can you provide an example of the diff command for comparing directories?

To compare two directories using the diff command, use the “-r” option followed by the names of the two directories. For example: diff -r dir1/ dir2/.

How do I use the diff command to show differences side by side?

To display the differences between two files side by side, use the “-y” option followed by the names of the two files. For example: diff -y file1.txt file2.txt.

Is it possible to compare binary files with the diff command in Linux?

Yes, it is possible to compare binary files with the diff command in Linux. However, you need to use the “-a” option to force diff to treat the files as text. For example: diff -a file1.bin file2.bin.

What is the difference between the diff and comm commands in Linux?

The diff command compares two files line by line and displays the differences between them, while the comm command compares two sorted files line by line and displays the lines that are common, unique to the first file, or unique to the second file.

Last Updated on January 16, 2024 by admin

Share this post on social

Copyright 2022 Linuxguidehq.com. All rights reserved. No part of this website may be reproduced, distributed, or transmitted in any form or by any means, including photocopying, recording, or other electronic or mechanical methods, without the prior written permission of the copyright owner.