
Do you need to see the differences between two revisions of a text file? Subsequently diff
is the command you need. This tutorial shows you how to use diff
on Linux and macOS, the simplest way.
Buceando in diff
the diff
The command compares two files and produces a list of the differences between the two. To be more precise, outputs a list of the changes that need to be made to the first file to match the second file. If you take this into account, it will be easier for you to understand the result of diff
. the diff
The command was designed to find differences between source code files and produce output that could be read and acted upon by other programs., As the patch command. In this tutorial, we will look at the most useful and human-friendly ways to use diff
.
Let's dive in and analyze two files. The order of the files on the command line determines which file diff
considers that it is the “first file” and which considers that it is the “second file”. In the following example, alpha1 is the first file and alpha2 is the second file. Both files contain the phonetic alphabet but the second file, alpha2, had some additional modifications so that the two files are not identical.
We can compare the files with this command. Writes diff
, a space, the name of the first file, a space, the name of the second file and then press Enter.
diff alpha1 alpha2
How do we dissect that result? Once you know what to look for, it won't be so bad. Each difference is listed in turn in a single column and each difference is labeled. Label contains numbers on both sides of a letter, What 4c4
. The first number is the line number in alpha1 and the second number is the line number in alpha2. The middle letter can be:
- C: The line of the first file should be changed to match the line of the second file.
- D: The line from the first file needs to be removed to match the second file.
- a: Additional content must be added to the first file to match the second file.
the 4c4
in our example, tell us that line four of alpha1 needs to be changed to match line four of alpha2. This is the first difference between the two files that diff
meetings.
Lines starting with <
check the first file, in our example alpha1, and the lines that start with >
check the second file, alpha2. The line < Delta
tells us that the word Delta is the content of line four in alpha1. The line > Dave
tells us that the word Dave is the content of line four in alpha2. Then, to sum up, we need to replace Delta with Dave on line four in alpha1, for that line to match in both files.
The next change is indicated by the 12c12
. Applying the same logic, this tells us that the line 12 in alpha1 it contains the word Lima, but the line 12 from alpha2 contains the word Linux.
The third change refers to a line that has been removed from alpha2. The label 21d20
is deciphered as “the line 21 needs to be removed from the first file for both files to sync from the line 20 onwards”. the < Uniform
line shows us the content of the line to be removed from alpha1.
The fourth difference is labeled 26a26,28
. This change refers to three additional lines that have been incorporated into alpha2. Note the 26,28
on the label. Two-line numbers separated by a comma represent a range of line numbers. In this example, the range goes from the line 26 to 28. The label is interpreted as “on the line 26 from the first file, add the lines 26 a 28 of the second file”. We are shown the three lines in alpha2 that must be added to alpha1. These contain the words Quirk, Strange y Charm.
Single line rapids
If all you want to know is whether two files are the same, Use the -s
(report identical files) option.
diff -s alpha1 alpha3
You can use the -q
(short) option to get an equally concise statement about two files that are different.
diff -q alpha1 alpha2
One thing to pay attention to is that with two identical files the-q
The option (short) closes completely and reports nothing at all.
An alternative view
the -y
(Side to side) uses a different layout to describe file differences. It is often convenient to use the -W
(width) with the view side by side, to limit the number of columns displayed. This avoids unsightly wraparound lines that make the output difficult to read. Here we have said it diff
to produce a side-by-side display and limit the output to 70 columns.
diff -y -W 70 alpha1 alpha2
The first file on the command line, alpha1, is displayed on the left and the second line of the command line, alpha2, shown on the right. The lines of each file are displayed, one beside the other. There are indicator characters next to those lines in alpha2 that have been changed, removed or added.
- |: A line that has been changed in the second file.
- <: A line that has been removed from the second file.
- >: A line that has been added to the second file that is not in the first file.
If you prefer a more compact parallel summary of file differences, Use the --suppress-common-lines
option. This forces diff
to list only modified lines, added or removed.
diff -y -W 70 --suppress-common-lines alpha1 alpha2
Add a splash of color
Another utility called colordiff
adds color highlighting to the diff
production. This makes it much easier to see which lines have differences.
Use apt-get
to install this package on your system if you are using Ubuntu or other Debian based distribution. On other Linux distributions, use the package management tool of your Linux distribution.
sudo apt-get install colordiff
Use colordiff
just as you would use it diff
.
Actually, colordiff
it is a wrapper for diff
, and diff
does all the work behind the scenes. It is because of that, all the diff
alternatives will work with colordiff
.
Provide some context
To find a middle definition between having all the lines in the files displayed on the screen and having only the changed lines in the listing, we can ask diff
to provide some context. There are two ways to do this. Both ways accomplish the same purpose, which is to show some lines before and after each modified line. You will be able to see what is happening in the file where the difference was detected.
The first method uses the -c
(copied context) option.
colordiff -c alpha1 alpha2
the diff
the output has a header. The header lists the two filenames and their modification times. There are asterisks (*
) before the name of the first file and hyphens (-
) before the second file name. Asterisks and hyphens will be used to indicate which file the output lines belong to.
A line of asterisks with 1,7 in the middle indicates that we are seeing lines of alpha1. to be precise, we are looking at lines one to seven. The word Delta is marked as modified. Has an exclamation mark ( !
) together with him, and it's red. Three lines of unchanged text are shown before and after that line so we can see the context of that line in the file.
The dashed line with 1,7 in the middle it tells us that we are now seeing alpha2 lines. Again, we are looking at lines one to seven, with the word Dave on line four marked different.
Three lines of context above and below each change is the default. You can specify how many lines of context you want diff
To provide. To do this, use el -C
(copied context) with a “C” uppercase and provide the number of lines you want:
colordiff -C 2 alpha1 alpha2
The second diff
The option that provides context is the -u
option (unified context).
colordiff -u alpha1 alpha2
As before, we have a header in the output. The two files are named and their modification times are displayed. There are scripts (-
) before the alpha1 name and plus signs (+
) before alpha2 name. This tells us that hyphens will be used to refer to alpha1 and plus signs will be used to refer to alpha2. Scattered throughout the list are lines that start with signs (@
). These lines mark the beginning of every difference. They also tell us which lines are shown from each file.
The three lines are shown before and after the line marked as different so that we can see the context of the modified line. In the unified view, the lines with the difference are displayed one on top of the other. The alpha1 line is preceded by a dash and the alpha2 line is preceded by a plus sign. This screen achieves in eight lines what the context screen copied above took fifteen to do.
How could we wait, we can ask diff
to provide exactly the number of lines of unified context that we would like to see. To do this, use el -U
(unified context) with a “U” uppercase and provide the number of lines you want:
colordiff -U 2 alpha1 alpha2
Ignore whitespace and case
Let's analyze two other files, test4 and test5. These have the six names of superheroes on them.
colordiff -y -W 70 test4 test5
The results show that diff
does not find anything different with the Black Widow lines, Spider-Man y Thor. Mark changes with Captain America lines, Ironman y Hulk.
Then, What is different? Well, a test5, Hulk is written with a “h” lowercase and Captain America has an additional space between “Captain” and “America”. It's okay, that's easy to see, but what about the ironman line? There are no visible differences. Here is a good rule of thumb. If you can't see it, the solution is a blank space. There is almost certainly a wasted space or two, or a tab character, at the end of that line.
If you don't care, you can instruct diff
to ignore specific types of line difference, including:
- -I: Ignore the differences between upper and lower case.
- -WITH: Ignore trailing blanks.
- -B: Ignore changes in the amount of white space.
- -w: Ignore all whitespace changes.
Let's ask diff to check those two files again, but this time to ignore any difference in the case.
colordiff -i -y -W 70 test4 test5
Lines with “The Hulk” and “The Hulk” are now considered a coincidence, and it makes no difference to the “h” lower case. Let's ask diff
to also ignore trailing whitespace.
colordiff -i -Z -y -W 70 test4 test5
As suspected, the final blank must have been the difference in the Ironman line because diff
no longer makes a difference for that line. That leaves Captain America. Let's ask diff
ignore the case and ignore everybody whitespace issues.
colordiff -i -w -y -W 70 test4 test5
counting diff
ignore differences that don't concern us, diff
tells us that, for our purposes, the files match.
the diff
The command has many more options, but most of them relate to producing machine-readable results. These can be reviewed on Linux man page. The alternatives we have used in the examples above will allow you to track down all the differences between the versions of your text files, using the command line and human eyes.
setTimeout(function(){
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq = n;n.push=n;n.loaded=!0;n.version=’2.0′;
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s) } (window, document,’script’,
‘https://connect.facebook.net/en_US/fbevents.js’);
fbq(‘init’, ‘335401813750447’);
fbq(‘track’, ‘PageView’);
},3000);