Linux-komennot

diff Command Examples in Linux

diff Command Examples in Linux
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:

# nano diffsample1

Inside paste:

 LinuxHint publishes the best content for you 

Press CTRL+X and Y to save and exit.

Create a second file called diffsample2 with the same content:

# nano diffsample2

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

Press CTRL+X and Y to save and exit.

# diff diffsample1 diffsample2

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:

# nano diffsample2

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

Press CTRL+X and Y to save and exit.

Now run:

# diff diffsample1 diffsample2

Lets see the output:

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 < belongs to the first file and 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

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:

Now let's edit the file diffsample1 like this:

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

# diff diffsample2 diffsample1

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

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:

And edit the file diffsample1 as:

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

# diff diffsample1 diffsample2

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

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:

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:

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

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:

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

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.

Microsoft Sculpt Touch Wireless Mouse Review
I recently read about the Microsoft Sculpt Touch wireless mouse and decided to buy it. After using it for a while, I decided to share my experience wi...
AppyMouse On-screen Trackpad and Mouse Pointer for Windows Tablets
Tablet users often miss the mouse pointer, especially when they are habitual to using the laptops. The touchscreen Smartphones and tablets come with m...
Middle mouse button not working in Windows 10
The middle mouse button helps you scroll through long webpages and screens with a lot of data. If that stops, well you will end up using the keyboard ...