The diff command in Linux is a utility tool that allows you to compare files from the command line. It displays the result on your terminal and provides several instructions to modify a file from the command line. You will need to understand the symbols and options to use this command. In this article, I will explain the frequent uses of the diff command with practical examples.
A. Description
The diff command in Linux compares files line by line. It can also differentiate between the contents within directories.
B. Syntax
The syntax of the diff command in Linux simply contains single or multiple options and then the desired Filenames.
diff [OPTION]... FILES
C. Options
The diff command in Linux offers several options to modify your experience of differentiating files. However, you will find the most useful ones here for your convenience. For any further features, you can always look at the man page.
man diff
Useful Options
- -B ( Ignores blank lines )
- -c ( Displays copied context )
- -i ( Ignores case differences )
- -r ( Recursively compares subdirectories )
- -s ( Reports if files are identical )
- -q ( Reports if files are different )
- -u ( Displays unified context )
- -w ( Ignores all white spaces )
- -y ( Displays results in two columns )
D. Symbols
The diff command in Linux uses a number of symbols to represent the differences between two files. The symbols also indicate instructions. Below are some symbols used by the diff command. You will find more information on the man page of the command.
man diff
Useful Symbols
- a/+ ( Indicates addition of content )
- c/! ( Indicates change of content )
- d/- ( Indicates deletion of content )
- > ( Lines from 1st File )
- < ( Lines from 2nd File )
Practical Examples of the “diff” Command in Linux
The diff command in Linux is a convenient command used to find the similarities or dissimilarities between file contents. Some of the most helpful applications of the diff command have been illustrated here. You can download the files associated with the examples from below.
Example 1: Displaying Differences Between Files and Instructions to Make the 1st File Similar
You can display differences between files using the diff command in Linux. The command will also give you instructions using symbols to reduce differences. In this example, I will demonstrate how the diff command presents differences and instructions. I will use two text files “file1.txt” and “file2.txt”. To do so you can follow the steps below.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
diff file1.txt file2.txt
➌ Now, press the ENTER button.
Output >
In this first image, I have shown you the contents of the file using the cat command for better understanding.In this second image, you can see that the diff command resulted in three instructions for the differences encountered in each line. All the instructions are only applicable to the 1st file “file1.txt”.
Explanations of each instruction are given below.
- 0a1: After line 0 ( at the beginning of the file ) add(a) the 1st line from the second file which is “Dhaka”.
- 2c3: Change(c) the 2nd line “Cumilla” into the 3rd line “Comilla” of the second file.
- 4,5c5,6: Change(c) the 4th,5th lines “Chattogram”, “Kushtia” into the 5th,6th lines of the second file. Lines from the second file are “Chittagong” and “Khulna”.
Example 2: Displaying the File Differences and Instructions in Separate Contexts
You can display differences between files in the file’s default context using the diff command in Linux with option -c. The command will show you dissimilarities between the 1st file with the 2nd file using symbols. These symbols also indicate instructions for making the files similar. In this example, I will differentiate two text files: “file1.txt” and “file2.txt” in copied context and explain the resulting symbols. To do so you can follow the steps below.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
diff -c file1.txt file2.txt
➌ Now, press the ENTER button.
Output >
In this first image, I have shown you the contents of the file using the cat command for better understanding.In this second image, you can see that the diff command is showing two kinds of symbols for the differences encountered in each line. The instructions given using the symbols are only applicable to the 1st file “file1.txt”.
Explanations of each symbol are given below.
- +: Add(+) “Dhaka” to the corresponding line of the first file.
- !: Change(!) the lines from the first file into the (!) assigned lines of the second file.
Similar Readings
- The “pwd” Command in Linux [4 Practical Examples]
- The “cd” Command in Linux [6 Practical Examples]
- The “ls” Command in Linux [7+ Practical Examples]
Example 3: Displaying the File Differences and Instructions in a Unified Context
Using the diff command in Linux with option -u you can display differences between files in a unified context. The command will show you dissimilarities between the 1st file with the 2nd file using symbols. These symbols also indicate instructions for making the files similar. In this example, I will differentiate two text files: “file1.txt” and “file2.txt” in copied context and explain the resulting symbols. To do so you can follow the steps below.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
diff -u file1.txt file2.txt
➌ Now, press the ENTER button.
Output >
In this first image, I have shown you the contents of the file using the cat command for better understanding.In this second image, you can see that the diff command is showing two kinds of symbols for the differences encountered in each line. The instructions given using the symbols are only applicable to the 1st file “file1.txt”.
Explanations of each symbol are given below.
- +: Add(+) the (+) assigned lines to the first file.
- –: Delete(-) the (-) assigned lines from the first file.
Example 4: Printing Out File Differences Side by Side Using the “diff” Command in Linux
Using the diff command in Linux with the option -y you can display differences between files in side-by-side columns. The command assigns symbols to the contents of the second file to indicate the differences. These symbols also represent instructions for making the files similar. In this example, I will display the differences between the two text files: “file1.txt” and “file2.txt” in adjacent columns. You can do the same by following the given steps.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
diff -y file1.txt file2.txt
➌ Now, press the ENTER button.
Output >
In this image, I have shown the contents of the files in side-by-side columns. The differences are indicated by (>) and (|) symbols.
Example 5: Checking If Two Files Differ Using the “diff” Command in Linux
You can check if two files are different using the diff command in Linux with the option -q. In this example, I will check if the two text files “file3.txt” and “file4.txt” are different. To do so you can follow the steps below.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
diff -q file3.txt file4.txt
➌ Now, press the ENTER button.
Output >
In the image below, at first, I showed the contents of the file using the cat command. Then I displayed that the two files are different.
Example 6: Checking If Two Files are Identical Using the “diff” Command in Linux
You can check if the contents in two files are the same using the diff command in Linux with the option -s. In this example, I will check if the two text files “file5.txt” and “file6.txt” are identical. To do so you can follow the steps below.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
diff -s file5.txt file6.txt
➌ Now, press the ENTER button.
Output >
In the image below, at first, I showed the contents of the file using the cat command. Then I displayed that the two files are identical.
Example 7: Case-Insensitive File Comparisons Using the “diff” Command in Linux
You can compare the contents in two files ignoring the case using the diff command in Linux with the option -i. In this example, I will compare the two text files “file3.txt” and “file4.txt” in a case-insensitive manner. To do so you can follow the steps below.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
diff -i -y file3.txt file4.txt
➌ Now, press the ENTER button.
Output >
In the image below, you can see that I have displayed the results side-by-side using the -y option. The command ignores cases and only differentiates the last two lines which are not included in the first file “file3.txt”.
Example 8: Ignoring White Spaces While Differentiating Files Using the “diff” Command in Linux
You can compare the contents in two files ignoring white spaces using the diff command in Linux with the option -w. In this example, I will compare the two text files “file7.txt” and “file8.txt” while ignoring spaces in a line. You can do so by following the steps below.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
diff -w -s file7.txt file8.txt
➌ Now, press the ENTER button.
Output >
In the image below, at first, I showed the contents of the file using the cat command. Then I checked if the files become identical ignoring white spaces using the -s option. The result shows that the files are identical.
Similar Readings
- The “mv” Command in Linux [8 Practical Examples]
- The “cp” Command in Linux [6 Practical Examples]
- The “rm” Command in Linux [7 Practical Examples]
Example 9: Ignoring Blank Lines While Differentiating Files Using the “diff” Command in Linux
You can compare the contents in two files ignoring blank lines using the diff command in Linux with the option -B. In this example, I will compare the two text files “file9.txt” and “file10.txt” while they have blank lines. You can do so by following the steps below.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
diff -B -s file9.txt file10.txt
➌ Now, press the ENTER button.
Output >
In the image below, at first, I showed the contents of the file using the cat command. Then I checked if the files became identical ignoring blank lines using the -s option. The result shows that the files are identical.
Example 10: Comparing Two Directories Using the “diff” Command in Linux
Using the diff command in Linux you can display differences between the contents in two directories. The command prints out the unique files and folders in each directory. In this example, I will differentiate two text directories: “Desktop” and “Documents”. To do so you can follow the steps below.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
diff Desktop Documents
➌ Now, press the ENTER button.
Output >
In the image below, at first, I showed the contents of the directories using the ls command. Then I displayed the different contents available in each directory.
Example 11: Redirecting Output of the “diff” Command in Linux
Using the diff command in Linux you will be able to write the command output in a desired file. For this, you will have to apply the Redirection (>) feature in Linux. In this example, I will redirect the differences between the two text directories: “Desktop” and “Documents” in a file called “diff.txt”. To do so you can follow the steps below.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
diff Desktop Documents > diff.txt
➌ Now, press the ENTER button.
Output >
In the image below, at first, I showed the contents of the directories using the ls command. Then I redirected the output in the “diff.txt” file. You can see the results redirected to the “diff.txt” file using the cat command.
Conclusion
In this article, I illustrated the common uses of the diff command in Linux. You need to develop an understanding of the symbols and instructions to use this command. I hope these practical examples will help you compare files from the command line and aid your experience with Linux.
Similar Readings
- The “touch” Command in Linux [8 Practical Examples]
- The “mkdir” Command in Linux [6+ Practical Examples]
- The “locate” Command in Linux [7 Practical Examples]
- The “find” Command in Linux [10+ Practical Examples]
- The “chmod” Command in Linux [6 Practical Examples]
- The “chown” Command in Linux [8 Practical Examples]
FUNDAMENTALS A Complete Guide for Beginners