Skip to content

diff command in Linux

The diff command in Linux is used to compare two files or directories and display the differences between them. It is a powerful tool that helps users identify changes, additions, and deletions in text files. By default, the command outputs a list of line numbers that differ between the two files. Users can also specify options to view a side-by-side comparison or unified diff format. With the diff command, users can easily identify discrepancies between files and make necessary changes to keep them in sync.

diff Syntax:

Terminal window
diff [option] [file1] [file2]

Linux diff Options:

OptionDescription
-qReport only whether the files differ
-sReport when two files are the same
-iIgnore case differences
-wIgnore white space
-BIgnore blank lines
-rRecursively compare any subdirectories found

diff Parameters:

ParameterDescription
file1The first file to compare
file2The second file to compare

How to use diff command:

Compare two files

Terminal window
diff file1.txt file2.txt

This command compares two files line by line and displays the differences.

Ignore whitespace changes

Terminal window
diff -b file1.txt file2.txt

The -b option ignores changes in the amount of whitespace.

Output differences in unified format

Terminal window
diff -u file1.txt file2.txt

The -u option displays the differences in a unified format.

Ignore changes in the amount of spaces

Terminal window
diff -w file1.txt file2.txt

The -w option ignores changes in the amount of spaces.

Show only which files differ in directories

Terminal window
diff -q directory1 directory2

The -q option shows only which files differ in directories without displaying the actual differences.

Compare two directories recursively

Terminal window
diff -r directory1 directory2

This command compares two directories recusively, showing differences found in files within the directories.

Create a patch file

Terminal window
diff -u originalfile revisedfile > changes.patch

This command creates a patch file that contains the differences between two files in unified diff format.

Apply a patch file

Terminal window
patch -p1 < changes.patch

This command applies the changes in a patch file to the original file.

How do I use diff in Linux?

To use the diff command in Linux, execute the following command:

Terminal window
diff file1.txt file2.txt

What are the options available with the diff command?

The diff command in Linux provides various options to customize its behavior. Here is an example of using the -u (unified) option:

Terminal window
diff -u file1.txt file2.txt

How can I ignore leading white space when using diff?

To ignore leading white space when using diff, you can utilize the -b option. Here’s an example:

Terminal window
diff -b file1.txt file2.txt

How do I get a brief output with the diff command?

To get a brief output showing only whether the files differ, you can use the -q option. Here is an example:

Terminal window
diff -q file1.txt file2.txt

Can I use diff to compare directories in Linux?

Yes, you can compare directories in Linux using the diff command with the -r option. Here’s an example:

Terminal window
diff -r directory1 directory2

How can I make diff ignore changes in the amount of white space?

To make diff ignore changes in the amount of white space, you can use the -w option. Here is an example:

Terminal window
diff -w file1.txt file2.txt

How can I display line numbers for differences in diff output?

To display line numbers for differences in the output of the diff command, you can use the -n option. Here’s an example:

Terminal window
diff -n file1.txt file2.txt

How do I suppress the normal output and only display differences in context format?

To suppress the normal output and only display differences in context format, you can use the -c option. Here is an example:

Terminal window
diff -c file1.txt file2.txt

Can I generate a patch file using the diff command?

Yes, you can generate a patch file using the diff command in Linux. Here’s an example of creating a patch file:

Terminal window
diff -u original_file updated_file > mypatch.patch

Applications of the diff command

  • Comparing two files
  • Showing differences between two files
  • Generating patches for file differences
  • Verifying whether two files are identical
  • Finding changes between directories