Everything is considered a file in Linux. A Linux computer has thousands of files that contain different kinds of content. You may need to analyze some of these files and show the differences among certain files for your development needs. Linux provides the diff command to compare the contents of two files on your Linux computer.
In this tutorial, we will learn how to use the Linux diff command to compare two files.
Prerequisites
A working Linux computer with terminal access.
Basic familiarity with Linux command-line interface.
Eagerness to learn new Linux commands and try these examples.
Diff Command in Linux
The diff command in Linux is used to compare two files and list their differences. This command compares two files line by line. You can also use the Linux diff command to compare the contents of Linux directories. The output of the Linux diff command provides instructions on how to update the first file to match it to the second file.
You can use the diff command in Linux to create a patch that contains differences between two files. This patch can be applied using the patch command in Linux. Diff also plays a significant role in scripting where you can run certain commands based on the differences between compared files.
Diff Command Syntax
The basic syntax of the diff command in Linux is as follows:
diff [option] file1 file2
Understanding the Diff Command in Linux Output
When you work on the diff command in Linux, it is important to know how to read the diff command output, which includes:
Output line that starts with < denotes the first file content.
Output line that starts with > denotes the second file content.
The following special symbols indicate how you can modify the first file to match the second file:
a (add) – content needs to be added.
c (change) – something needs to be changed.
d (delete) – something needs to be deleted.
Diff Command Options
The diff command in Linux provides various options that you can use to modify the command behavior and display output in different formats. The following table describes some of the most used Linux diff command options:
Options
Description
-c
Displays output in the context format.
-u
Display output in the unified format ignoring redundant context lines.
-i
Ignores cases in the file contents.
-a
Views files as plain text files and compares two files line by line.
-b
Ignores white spaces before empty output lines while comparing the files.
-B
Ignores blank lines while comparing the files.
--binary
Compares and writes file content in binary mode.
-e
Creates a valid ed script from the output.
-E
Ignores tab expansion while comparing the files.
-N
Treats a file as present but empty if the file is missing.
-q
Specifies if the files differ, without providing exact differences.
-s
Shows that the files are identical.
-w
Ignores white spaces completely.
Linux Diff Command Examples
The Linux diff command output can be displayed in the following three formats:
Normal format- This is the default output that is displayed when you compare two files without specifying any other option.
Context format- This output format displays lines of context about the text that is different in the compared files.
Unified format- This output format is an improved form of the context format output and it displays a smaller output.
To better explain how the Linux diff command works, we are creating two sample files that we will use in the examples in this tutorial:
1. Create the first file capitals.txt using the nano editor:
nano capitals.txt
2. Add the following lines to the first file:
Beijing
Tokyo
Mumbai
Moscow
3. Save the changes and exit.
4. Create the second file cities.txt using the nano editor:
nano cities.txt
5. Add the following lines to the second file:
New York
Los Angeles
Chicago
Huston
Phoenix
6. Save the changes and exit.
1. Compare in Normal Format
To compare the files in the normal format, specify the file names after the diff command:
diff capitals.txt cities.txt
The normal diff command output provides instructions on modifying the first file to make it identical to the second file. Let’s review the output and translate the instructions in human-readable format:
1d0 – You must delete the first line from the first file, otherwise, it will seem in line number 0 in the second file.
< Beijing – The text that needs to be deleted, is specified as 1d0.
3c2 – In line number 3 of the first file, you must change line 2 from the second file.
< Tokyo – The text you need to change.
> Los Angeles – The text you need to change it to.
5c4,5 – In line 5 of the first file, you must change line numbers 4 and 5 from the second file.
< Moscow – The text you need to change.
> Huston – The text you need to change it to.
> Tokyo - The text you need to change it to.
You can use the patch command after generating the output instructions to save the diff command output and apply the changes.
2. Compare in Context Mode
The default output of the diff command is quite complex to understand. You can modify this output to the context mode to make it easy to understand. Use the -c option with the diff command to display lines of context around the content that is different:
diff -c capitals.txt cities.txt
In this output, the lines starting with *** display information about the first file, and the lines starting with --- display information about the second file. The initial two lines of the output of the diff command display file names and modification dates. Then we have the ********** separator.
After that, we have the line range (1,5) and differences for the first file. The initial symbol indicates the change that we need to make to the first file and the rest of the line is the content. You can see the following three symbols:
- or minus – You need to delete this content.
! or exclamation mark – You need to replace this content with the corresponding line in the second file.
+ or plus – You need to add this text to the first file.
If a line does not start with a symbol, then it does not require any modification.
3. Compare in Unified Format
Use the -u option to display the output in a unified format where the redundant context lines are ignored:
diff -u capitals.txt cities.txt
In this output, the lines starting with --- display information about the first file and the lines starting with +++ display information about the second file. Below that, @@ -1,5 +1,5 @@ represents the line numbers range of both the files.
The next lines display the list of changes that you need to make to make both the files identical. You can see the following two symbols:
- or minus – You need to delete this content.
+ or plus – You need to add this text to the first file.
If a line does not start with a symbol, then it does not require any modification.
4. Ignore Case Using -i Option
The diff command in Linux, like all other Linux commands, is case-sensitive by default. This means that if the only difference between the specified file is the casing of the text, then also you will see the difference in the files. However, you can use the -i option, to ignore the case difference in the content in the two files.
For example, let's create a new file named metros.txt:
nano metros.txt
Then add the following city names to this file with two entries starting with a lowercase letter:
Beijing
New York
tokyo
Mumbai
moscow
Without any additional option, the output of the diff command shows that these files are different and lists the changes we need to make them identical:
Use the -i option to ignore casing and display results that are different in content, not just the case:
Now we don’t see any differences because the content is not different.
5. Using Patch with Diff
In a production environment, the files are updated frequently, and it is extremely difficult to keep track of all these changes. Using the diff command in combination with the patch provides you an option to directly apply the changes to the target files.
The first step is to save the differences between two files into a new file:
diff -u capitals.txt cities.txt > correction.txt
Then we check the content of the correction.txt file:
cat correction.txt
Once we have the list of changes, you can apply the patch to the second file as follows:
patch cities.txt < correction.txt
Check the contents of the second file to make sure it is identical to the first file:
cat cities.txt
Conclusion
In this tutorial, we learned how to use the diff command in Linux to compare two files and list the differences. We have included multiple examples in this article that will help you better understand how the diff command works. You can also use the patch in combination with the diff command to automate the changes to make the compared files identical.
If this resource helped you, let us know your care by a Thanks Tweet.
Did you find this article helpful?
We are glad you liked the article. Share with your friends.
About The Author
Subhash Chandra
Subhash Chandra is a professional writer and an Oracle-certified Database Administrator with over 15 years of writing experience. He has a passion for technology and loves to write how-to articles for Linux, Windows, Mac OS, Networking, Telecom, and Database. In his spare time, he enjoys swimming and playing table tennis.
Comments