compare two files in linux

How to Compare Two Files in Linux

Often we are required to compare two files, whether it is source code or simple text files. It can be tedious to manually go through each file and figure out the differences in them. Linux provides many useful tools to compare files. In this article, we will look at how to compare two files in Linux using diff command as it is the most common and popular tool used for this purpose.


How to Compare Two Files in Linux

We will look at how to compare two files using diff command in Linux.


1. Using diff

Diff is the most common tool used in Linux to compare files. Here is the syntax to use diff command to compare two files.

$ diff first_file_path second_file_path

We need to specify both the file paths after diff command. If you don’t specify the full file path, then diff command will look for the files in your present working directory. The diff command compares the first file with the second file and highlights the changes to be made in first file to make it identical to the second file.

It displays the output using 3 letters – a for addition, c for change, and d for deletion. Also < is used to indicate the first file and > is used to indicate second file.

Here is an example of diff command. Let us create 2 files. First we create test1.txt

$ sudo vi test1.txt

Add the following lines to it.

hello world
good morning

Create another file test.txt

$ sudo vi test2.txt

Add the following lines.

hello world

Then we run diff command.

$ diff test1.txt test2.txt
2d1
<good morning

In the above example, the output 2d1 means that the second line in file 1 has to be deleted to match first line of file 2.


diff command options

diff command also provides options to simply its output.

$ diff -c test1.txt test2.txt
*** test1.txt   2021-08-26 04:01:10.244465867 +0000
--- test2.txt   2021-08-26 04:01:22.796548841 +0000
***************
*** 1,2 ****
  hello world
- good morning
--- 1 ----

In this case, called context mode, diff command also compares the creation date of both files. The firs file is indicated by *** and the second file is indicated by —. The *************** is just a separator. It lists the content of first file between lines starting with *** and —, and the contents of second file after line starting with —. On each line, it will display +, – or no sign. – sign added at the beginning of line indicates that line needs to be deleted, while + sign indicates that it needs to be added. If there is no symbol at the beginning of a line, it means no change needed. In above example, we need to delete the line ‘good morning’ from first file.

You may also use unified mode using -u option for comparison. It is similar to context mode but does not show any unnecessary information. So y

$ diff -u test1.txt test2.txt
--- test1.txt   2021-08-26 04:01:10.244465867 +0000
+++ test2.txt   2021-08-26 04:01:22.796548841 +0000
@@ -1,2 +1 @@
 hello world
-good morning

In this case it first displays the file names with their creation dates. Then it starts showing the differences after line starting with @@.

First it lists all common lines in both files. Then it displays lines that need to be added/deleted in first file. Then it shows lines to be added/deleted in second file. If a line has + sign at its beginning, it needs to be added. If it has – sign at its beginning it needs to be deleted.

Here are some more common options for diff command

  • e – show output as ed script
  • y – output in colors
  • q – give output only if there is any difference

In this article, we have looked at how to compare two files in Linux using diff command.

Also read:

How to Use Strace Command in Linux
Shell Script to Check Disk Space & Send Email Alerts
Shell Script to Check Memory & Send Email Alerts
Linux Split File into Multiple Files
How to Schedule Reboot in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *