Hi there, In this tutorial we are going to learn how to compare two files in Linux. In order to compare two files in Linux, mainly two commands are used the “diff” command and the “cmp” command.
The diff and cmp commands are part of file manipulation in Unix. These both commands have many options that can be used by Linux users for many different tasks.
So, In this guide, we will not talk about the commands only but we will cover their options also.
Table of Contents
In order to follow this tutorial you need to have a computer system with a Linux-based operating system. It doesn’t matter which Linux distribution you are using because the commands we are sharing here will work on every Linux distro.
Also, you need to have two or more text files to compare with each other.
In such a case, if you do not know how to create files in Linux then you can learn it from here- Create a file in Linux.
And also need to know how to add text to a file.
Well, let’s start with the diff command.
Compare Files in Linux Using
The diff command compares two text files and lists the difference on the Linux terminal. It is the best command for comparing two text files and users use it for various purposes like comparing source code and many other things.
Note- When we use the diff command with no option then we get the output based on their order in the list. This means the first difference will be printed first.
Let’s check the syntax first and then the example-
diff [OPTION]... FILE_1 FILE_2
Let’s suppose, we have two files “SuperHero1.txt” and “SuperHero2.txt” which contain the list of some superheroes.
SuperHero1.txt is here-
Batman Iron Man Superman Thor Spider Man G.one
SuperHero2.txt is here-
Batman Iron Man Superman Krish Thor Spider Man
You can see, In the first file there is an Indian superhero name “G.one” and in the second file there is another Indian superhero name “Krish” but the uncommon. The “G.One” is not available in the first file and the “Krish” is not available in the second file.
Let’s compare it with the diff Linux command-
diff SuperHero1.txt SuperHero2.txt
Now assume what will be the output.
Well, The output is here-
Are you able to figure out why “Krish” is in the first position and “R.one” in the second?
It is because, When the diff command compared these both files it found “Krish” first which is in the second file.
cmp command in Linux for Comparing Files in Linux
The cmp command stands for comparison and it compares two files byte by byte. The cmp command is useful when a Linux user wants to find the location of the text mismatch occurs.
Let’s check the syntax then we will take an example-
cmp [FileName1] [ FileName2]
Let’s use SuperHero1.txt and SuperHero2.txt files for example again. Let’s compare these two files using The cmp Linux command-
cmp SuperHero1.txt SuperHero2.txt
After running the given command in the example, two files SuperHero1.txt and SuperHero2.txt will be compared with each other until a difference is found. Once the command finds a difference, the comparison will get stopped and output will be produced.
Basically, the cmp command will compare two files until it finds the first difference.
Once, it gets a difference it will print the byte and line number on the terminal.
The cmp Command Options
There are some useful options that you can use with the cmp command. Let’s have a look at the table-
|Print the differing byte
|Output the byte number and differ the byte value
|Compare at most LIMIT byte
|Suppress all normal output
|Output version information and exit
The Comm Command for Comparing Sorted Files
The comm command is useful when a Linux user wants to compare two sorted files line by line. If the files are not sorted in order then you can sort them (using the sort command) first and then use the comm command.
The comm command provides the output in three different columns. In the output, the first column displays the unique line of the first file. The second column displays the unique line in the second file. And the third column displays the common line in both files.
comm [FileName1] [FileName2]
Note- While comparing two files in Linux using the comm command make sure files are in sorted order otherwise you will get errors like-
comm: file 1 is not in sorted order
comm: file 2 is not in sorted order
If you are getting such an error then you may need to learn how to sort files in Linux first.
Let’s learn with an Example-
comm SuperHero1.txt SuperHero2.txt
In the output screenshot, you can see there are three columns. In column one a name is getting displayed which is “Ra.one” and in the second column another name getting displayed which is “Krish”. These two names “Ra.one and “Krish” are names that are not common in both files. However, there are a few names in column three that are common names in both files.
In this tutorial, we have explained how to compare files in Linux using the Linux terminal. We have shared three Linux commands to find the differences between the two files. The First command is the diff command the second is the cmp command and the third command is the comm command.