Dear Linux geeks,
In this article I will teach you how to use a very useful tool that can be used to compare files line by line. I find it easy to use and recommend it to Linux geeks that want to move their skills to the next level.
As many of unixmen readers may know, the diff command which comes installed by default in many Linux distributions is used to compare files line by line.
There are many options available in the diff command-line tool such as -r which is used to report when two files are the same, –i to ignore case differences in file contents, -E to ignore changes due to tab expansion and many others that we will cover in this tutorial.
Open a new terminal and use the vim or any text editor you like to create two text files in the Desktop directory that we will need to practice the diff command.
cd /home/oltjano/Desktop vim file1.txt vim file1.txt
Add the following text in the first file.
Then open the second file and add the text sh0wn below. Make sure to save the changes in both cases as we need the files to have text so we can compare them by using the diff command-line utility.
After you have created the file, the diff command line utility can be used as shown below to compare the two files with each other line by line and find the differences between them.
diff file1.txt file2.txt
After running the above command I get the following output printed in my Linux terminal.
2d1 < 13
As you guys can see the diff utility is performing very well. It detected the difference between the two files we created in the beginning of this tutorial and gave us information by printing it as standard output.
You can also paginate the output with the help of the -l option like shown below.
diff -l file1.txt file2.txt
It passes the output through `pr‘ to paginate it. But I like my output nice and shiny so I use the colordiff tool which is a tool that is used to colorize the diff output.
color-diff is available in the default repositories of Ubuntu, so the apt-get command-line tool can help me to install it in my own system.
Type the following command to install colordiff in your Ubuntu system.
sudo apt-get install colordiff
You can install the colordiff utility in rpm operating systems using the RPM Package Manager by running the following command in your terminal.
yum install colordiff
According to the man page the colordiff tool has been tested various flavors of Linux and under OpenBSD, but should be broadly portable to other systems. It makes use of ANSI colors.
But, what is the difference between diff and colordiff?
The only difference that I know is that colordiff produces the same output as diff but with colored syntax highlighting. I like the colored syntax very much and find both tools very useful when I need to compare two files, see the changes between one version of a file or compare two configuration or program files.
The following commands make use of the colordiff tool.
colordiff file1.txt file2.txt
But if you are geeky like me, you can use a pipeline and make use of both diff and colordiff tools.
diff file1.txt file2.txt | colordiff