This tutorial will show you how to use the diff command to compare files within the Linux terminal.
Knowing how to compare files within the Linux terminal is a great thing. It allows you to easily see what has changed between two files. For example, if you made a backup of a config file, you can see what has changed since that backup was created.
To compare two files on Linux, we will utilize a command called diff. This tool comes bundled with most Linux distributions, making it a great tool to learn how to use.
This tool allows you to compare two text files and output the lines that are different to the terminal.
You can even compare entire directories of files against another all within the terminal.
Best of all, as you will see over the next few sections, this tool is surprisingly simple to use.
Using the diff Command to Compare Files on Linux
In the following sections, we will show you how to use the diff command on Linux to compare files or directories.
While we won’t be touching on all aspects of this powerful tool, we will explore the most common ways to compare files.
Basic Syntax of the diff Command
The syntax of the diff command on Linux is very simple at its core. All you need to do at a minimum is specify the two files you want to compare one after the other, and it will output the differences.
By default, when comparing files, this command outputs the differences between them. Any whitespace or case difference will be detected as a difference and outputted to the command line.
diff [OPTIONS]... PATH1 PATH2
Below you can see the various parameters that the diff command expects.
[OPTIONS]
: Using this parameter, you can control how the diff command will output its comparisons. You can also use it to control how the comparisons between the two files are performed.PATH1
: Replace this placeholder with one of the files or directories you want to compare.PATH2
: Swap this out with the path to a file or directory you want to compare against.
Comparing Files with the Default Output
At its most basic usage, you only need to specify the two files or folders you want to compare on your Linux system.
For example, we can use the following command to compare the file “example1.txt
” file against the one called “example2.txt
“.
diff example1.txt example2.txt
Below, you can see the text that has been outputted using the diff command in Linux.
16c16
< p
---
> pimylifeup
The output of this command might look a little confusing at first glance, so let’s explore the various values and how they show the differences between files on your Linux system.
16c16
: On the left side, you will see the line number the change begins in the first file. The number on the right side indicates the line number in the second file.
In the middle of these two numbers, you will find a character that indicates what has occurred between these two files.a
: The letter “a
” indicates that lines were added at this position.c
: If you see the letter “c
“, you can tell a line has been modified.d
: Finally, if you see “d
” within the output, then the detected change is a line that has been deleted.
<
: The less than symbol indicates a line that has been removed from a file.>
: The greater than symbol shows that the line has been added to a file.---
: The three hyphens are a separator between files one and two.
Comparing Files using Context Mode
The diff command is a powerful tool on Linux that offers several different methods for outputting its comparisons. One of these modes is the “context mode”.
When using the context mode to compare files on Linux the tool will output the non-modified lines around the ones that have been changed.
To activate the context mode of the diff command, you will want to use the “-c
” option as shown below.
diff -c example1.txt example2.txt
Below you can see how the file comparison is output to the terminal.
*** example1.txt 2024-05-21 14:38:06.778489524 +1000
--- example2.txt 2024-05-21 16:21:26.490878256 +1000
***************
*** 11,25 ****
k
l
m
- n
o
! p
q
r
s
t
u
v
w
x
y
--- 11,25 ----
k
l
m
o
! pimylifeup
q
r
s
t
u
v
+ helloworld
w
x
y
Looking at the above output, you will see that by using the “context” option the diff command on Linux shows the data drastically different. Let us explore how you can interpret this alternative way of displaying the data comparison.
*** 11,25 ****
: The asterisk symbols indicate the first file that you are comparing. The numbers in the middle of the symbols are the line range.
The first number is the start of the shown block. The second is the line where this comparison ends.--- 11,25 ---
: Just like the above, but the dashes indicate that this block is a comparison of the second file.- Symbols: Below, you can see the symbols that are used to indicate which lines have changed between these two.
+
: The plus symbol tells you that this is a new line that has been added to the file.-
: The minus symbol will show you a line that has been removed when comparing the two files.!
: The final symbol is the exclamation mark. This indicates a line that has been changed between the two files.
Side by Side Comparison of Files in Linux
The diff command also allows you to compare files within Linux side by side. This is useful if you want a simple way to see where changes have been made within a file.
To get this tool to output the file comparison side by side, you will want to use the “-y
” option, as shown below.
diff -y example1.txt example2.txt
Below, you can see how the changes are output to the terminal.
a a
b b
c c
d d
e e
f f
g g
h h
i i
j j
k k
l l
m m
n <
o o
p | pimylifeup
q q
r r
s s
t t
u u
v v
> helloworld
w w
x x
y y
z z
The advantage of the side-by-side file comparison is that you can easily see how these two files differ. You will also see symbols on the right-hand side in addition to the text. These symbols indicate what the file change is.
Below, you can see what these symbols indicate about the changes made between the files.
<
: The less than sign indicates that the particular line it’s next to has been removed.|
: If you see the pipe symbol, you will know that this line has been modified.>
: Finally, the greater than sig shows a new line that has been added to the file.
Using the diff Command to Output the Comparison in Unified Mode
By using the “-d
” option with the diff command on Linux, you can get it to output in Unified mode.
Unified mode means the diff tool will output the differences between the two files within the same block.
diff -u example1.txt example2.txt
The result below shows the unified output produced by the diff command.
--- example1.txt 2024-05-21 14:38:06.778489524 +1000
+++ example2.txt 2024-05-21 16:21:26.490878256 +1000
@@ -11,15 +11,15 @@
k
l
m
-n
o
-p
+pimylifeup
q
r
s
t
u
v
+helloworld
w
x
y
Like the other methods, let us explain how you can interpret the file comparison data returned by the diff command on Linux.
@@ -11,15 +11,15 @@
: Each comparison block will start with two “at” symbols (@@
). This block is used to indicate where these changes are being compared to in their respective files-11,15
: The range noted by the minus symbol is the line range of the first file you are comparing.+11,15
: The plus symbol indicates the line range of the file we are comparing against.
- Symbols: Like the other comparison methods that the Linux diff command offers,
+
: The plus symbol indicates a line that has been added.-
: The minus symbol shows you a line that has been removed.
Using the diff Command to Compare Directories on Linux
One key functionality of the diff command is comparing directories on Linux.
The way you compare directories is exactly the same as you would with files, but the output differs slightly as it compares every file that exists in both directories.
Below, you can see that we are comparing a directory called “example1
” against one called “example2
“.
diff ./example1/ ./example2/
Below, you can see how the diff command compares each directory. For every file that the tool finds in both directories, it will display any differences within the terminal.
It will notify you if the file is only in one directory and not the other.
diff example1 example2
diff example1/example1.txt example2/example1.txt
2c2
< b
---
>
Only in example2: example2.txt
Case Insensitive File Comparison in Linux using the diff Command
Sometimes, there are changes that you don’t care about when comparing two files using Linux’s diff command. Luckily, this tool allows you to change the ways it compares files.
By default, the diff tool compares files case-sensitively. This means that it sees “pimylifeup
” and “PiMyLifeUp
” as different values. While this is useful for most cases, it isn’t for all.
If you aren’t worried about whether a change like this has occurred, you can use the “-i
” or “--ignore-case
” option.
diff -i example1.txt example2.txt
Ignoring White Space when Comparing
Changes in white space can also sometimes cause false differences to be displayed when comparing files. Luckily, Linux’s diff command has functionality that allows it to ignore any white space within a file.
By using the “-w
” or “--ignore-all-space
” options, the diff command will ignore any white space changes when searching the file for any differences.
diff -w example1.txt example2.txt
Conclusion
Hopefully, at this stage, you will have a good understanding of how to compare files on a Linux system using the diff command.
The diff command allows you to compare two text files line by line. It has many uses, such as comparing what you changed in a configuration file against an older backup.
Please feel free to leave a comment below if you have any questions or concerns with comparing files using this tool.
If you liked this tutorial, we highly recommend you check out our many other Linux tutorials.