The diff command is an analysis or informative command which prints differences between files, analyzing them line by line, or directories recursively while informing the user what changes are necessary to make files equals, this point is important to understand diff outputs. This tutorial focuses on the diff command.

Before starting, create two files using any text editor (nano is used in this tutorial) with the same content:

diff Command Examples in Linux Utilities

Inside paste:


LinuxHint publishes
the best
content for you

diff Command Examples in Linux Utilities

Press CTRL X and Y to save and exit.

Create a second file called diffsample2 with the same content:

diff Command Examples in Linux Utilities

diff Command Examples in Linux Utilities

Note: pay attention to spaces and tabs, files must be 100% equal.

Press CTRL X and Y to save and exit.

# diff diffsample1 diffsample2

diff Command Examples in Linux Utilities

As you can see there is no output, no need to do something to make files equal because they are already equal.

Now lets edit the file diffsample2 to make some change:

Then let’s replace the word “content” for “tips”:

diff Command Examples in Linux Utilities

Press CTRL X and Y to save and exit.

Now run:

# diff diffsample1 diffsample2

Lets see the output:

diff Command Examples in Linux Utilities

The output above, “3c3” means “Line 3 of first file of should be replaced for line 3 of second file”. The friendly part of the output is it shows us what text must be changed (“content for you” for “tips for you”)

This shows us the reference for the command diff isn’t the first file but the second one, that’s why the first file third line (the first 3) must be changed (C) as the third line of the second file (second 3).

The command diff can show 3 characters:

c: this character instructs a Change must be done.

a: this character instructs something must be Added.

d: this character instructs something must be Deleted.

The first numbers before a characters belong to the first file, while the numbers after characters belong to the second file.

The symbol to the second file which is used as reference.

Let’s invert the files order, instead of running

# diff diffsample1 diffsample2

run:

# diff diffsample2 diffsample1

diff Command Examples in Linux Utilities You can see how the order was inverted and now the diffsample1 file is used as reference, and it instructs us to change “tips for you” for “content for you”, this was the previous output:

diff Command Examples in Linux Utilities

Now let’s edit the file diffsample1 like this:

diff Command Examples in Linux Utilities

Remove all lines,except for the first line on the file diffsample1. Then run:

# diff diffsample2 diffsample1

diff Command Examples in Linux Utilities

As you can see, since we used the file diffsample1 as reference, in order to make the file diffsample2 exactly equal we need to delete (d) lines two and three (2,3) like in the first file and first lines (1) will be equal.

Now lets invert the order and instead of running “# diff diffsample2 diffsample1” run:

# diff diffsample1 diffsample2

diff Command Examples in Linux Utilities

As you can see, while the previous example instructed us to remove, this one instructs us  to add (a) lines 2 and 3 after the first file first line (1).

Now let’s work on the case sensitive property of this program.

Edit the file diffsample2 like:

diff Command Examples in Linux Utilities

And edit the file diffsample1 as:

diff Command Examples in Linux Utilities

The only difference are the capital letters on the file diffsample2. Now lets compare it using diff again:

# diff diffsample1 diffsample2

diff Command Examples in Linux Utilities

As you can see diff found differences, the capital letters, we avoid diff detecting capital letters, if we aren’t interested in the case sensitive by adding the -i option:

# diff -i diffsample1 diffsample2

diff Command Examples in Linux Utilities

No differences were found, the case detection was disabled.

Now let’s change the output format by adding the option -u used to print unified outputs:

diff Command Examples in Linux Utilities

Additionally, to date and time, the output shows with a and symbol what should be removed and what should be added in order to make files equal.

At the start of this article I said spaces and tabs must be equal in both files, since they are also detected by the command diff, if we want the command diff to ignore spaces and tabs we need to apply the -w option.

Open the file diffsample2 and add spaces and tabs:

diff Command Examples in Linux Utilities

As you see I added a couple of tabs after “the best” in the second line and also spaces in all lines, close,save the file and run:

# diff diffsample1 diffsample2

diff Command Examples in Linux Utilities

As you can see differences were found, additionally to the capital letters. Now lets apply the option  -w to instruct diff to ignore blank spaces:

diff Command Examples in Linux Utilities

As you see despite the tabulation diff only found as difference the capital letters.


Now let’s add the option -i again:

#diff  -wi diffsample2 diffsample1

diff Command Examples in Linux Utilities

The command diff has dozens of available options to apply to ignore, change the output, discriminate columns when present, etc. You can get additional information on these options using the man command, or at http://man7.org/linux/man-pages/man1/diff.1.html. I hope you found this article with diff Command Examples in Linux useful. Keep following LinuxHint for more tips and updates on Linux and networking.

About the author

diff Command Examples in Linux Utilities

Ivan Vanney

Ivan Vanney has over 2 years as writer for LinuxHint, he is co-founder of the freelance services marketplace GIGopen.com where he works as a sysadmin.