# How to use diff Command in Linux

## Introduction

The Linux operating system is based on a robust set of command line tools that allow users to easily manage and control the system. One of the most powerful and widely used command line utilities is the "diff" command. The "diff" command in Linux is used to compare the contents of two files line by line and show the difference between them. This command-line utility is essential for developers and system administrators to identify changes to code or configuration files. In this article, we'll take a closer look at the diff command and its various options and usage scenarios. We'll also provide easy-to-follow examples to help you understand how to use the diff command effectively.

## Comparing two files

The most basic use of the diff command is to compare the contents of two files. The syntax of this command is as follows −

$diff file1 file2  For example, suppose we have two files named "file1.txt" and "file2.txt" in our current directory. We can compare the contents of these two files by running the following command − $ diff file1.txt file2.txt
2c2
< line 2
---
> line 2 modified


The command output will show any differences between the two files, with lines unique to each file preceded by "+" or "-". In this example, the second line of "file2.txt" has been modified, as indicated by the "c" at the beginning of the output.

## Comparing Directory

The diff command can also be used to compare the contents of two directories. The syntax of this command is as follows −

$diff -r directory1 directory2  For example, suppose we have two directories named "dir1" and "dir2" in our current directory. We can compare the contents of these two directories by running the following command − $ diff -r dir1 dir2
diff -r dir1/file1.txt dir2/file1.txt
2c2
< line 2
---
> line 2 modified


The "-r" option tells diff to compare the contents of directories recursively, which means it will also compare the contents of any subdirectories. In this example, the output shows that the file "file1.txt" in "dir2" has been changed, with the same change as before.

## Ignoring white spaces

By default, the diff command will treat any whitespace changes as a difference. However, sometimes we may want to ignore these changes and only compare the meaningful content of the files. The "--ignore-space-change" option can be used to ignore whitespace changes.

For example, suppose we have two files named "file1.txt" and "file2.txt" in our current directory, with “file2.txt” having extra whitespace.

$diff --ignore-space-change file1.txt file2.txt  In this example, the output is blank, indicating that there is no difference between the two files ignoring the whitespace changes. ## Comparing using specific format The diff command can also be used to compare files in a specific format. The "--strip-trailing-cr" option can be used to ignore carriage returns at the end of lines. For example, suppose you have two files named "file1.txt" and "file2.txt" in the current directory, and “file2.txt” has an extra carriage return at the end of each line. $ diff --strip-trailing-cr file1.txt file2.txt


In this example, the output is blank, indicating that there is no difference between the two files ignoring carriage returns at the end of each line.

## Interpreting the Output

It is important to understand how to interpret the output of the diff command. The output consists of lines starting with "<" for the first file and ">" for the second file, line numbers corresponding to each file, and special symbols indicating how to change the first file to match the second file . such as "a" to add, "c" to change, and "d" to delete).

The "-c" option can also be used to display the differences between files as a context, by displaying several lines of context around the lines that differ. This can make it easier to understand the changes made between the two files.

## Conclusion

The diff command is a powerful tool for comparing the contents of two files or directories in Linux. It can be used to identify differences between files, ignore whitespace changes, and compare files in specific formats. With the various options available, the diff command can be customized to fit a wide range of use cases, making it an essential tool for developers and system administrators.