Atlantic.Net Blog

How to Compare Two Files in Linux Terminal

File comparison plays an important role in Linux, especially for programmers and Linux system administrators. For example, if you want to find the difference between two source code files to develop patches, then you need a file comparison tool to make this process easier. There are several command-line tools available in Linux to compare two files. Among them, diff is a very popular command-line utility that provides various options to get the difference between two files.

In this post, we will show you how to compare two files in a Linux terminal.

Basic Syntax

The basic syntax of the diff command is shown below:

diff [OPTION] FILES

A brief explanation of each option is shown below:

  • -s Report when two files are the same
  • -c Display output in context mode
  • -q Report only when files differ
  • -y Shows difference output in two columns
  • -r Recursively compare any subdirectories
  • -i Ignore case differences in file contents
  • -w Ignores all white space
  • –ignore-file-name-case Ignore case when comparing file names
  • –no-ignore-file-name-case Consider a case when comparing file names

Also Read

How to Download File Using Wget in Linux

Create Files in Linux

To perform file comparison in Linux, you will need to create some files in your system.

Let’s create a first file named file1.txt:

nano file1.txt

Add the following contents:

11 12 13 14 15 16 17 18 19
eveven twelve thirteen fourteen fifteen sixteen seventeen eighteen ninteen
In this file, we added some numbers in text and numerix form
Atlantic Cloud

Let’s create a second file named file2.txt:

nano file2.txt

Add the following contents:

10 12 3 14 8 16 7 18 19
five twelve eight fourteen fifteen nine seventeen one ninteen
In this file, we added some numbers in text and numerix form
Atlantic Cloud

Let’s create a third file named file3.txt with content similar to file1.txt:

nano file3.txt

Add the following contents:

11 12 13 14 15 16 17 18 19
eveven twelve thirteen fourteen fifteen sixteen seventeen eighteen ninteen
In this file, we added some numbers in text and numerix form
Atlantic Cloud

Also Read

How to Install and Use Ack Command in Linux

Print Differences Between Two Files

At this point, three files are ready for practical use with the diff command.

Now, to print the differences between two files named file1.txt and file2.txt, run:

diff file1.txt file2.txt

You will get the following output:

1,2c1,2
< 11 12 13 14 15 16 17 18 19
< eveven twelve thirteen fourteen fifteen sixteen seventeen eighteen ninteen --- > 10 12 3 14 8 16 7 18 19
> five twelve eight fourteen fifteen nine seventeen one ninteen

As you can see, the diff command has omitted all the similarities between the two files and only displayed their differences.

If you want to display output in context mode, use the -c option:

diff -c file1.txt file2.txt

You will get the following output:

*** file1.txt	2022-03-30 16:00:02.639907838 +0530
--- file2.txt	2022-03-30 16:00:23.588174892 +0530
***************
*** 1,4 ****
! 11 12 13 14 15 16 17 18 19
! eveven twelve thirteen fourteen fifteen sixteen seventeen eighteen ninteen
  In this file, we added some numbers in text and numerix form
  Atlantic Cloud
--- 1,4 ----
! 10 12 3 14 8 16 7 18 19
! five twelve eight fourteen fifteen nine seventeen one ninteen
  In this file, we added some numbers in text and numerix form
  Atlantic Cloud

Find Similar Files Using Diff Command

You can use the -s flag with the diff command to check whether two files are similar.

Run the following command to compare the file1.txt and file3.txt:

diff -s file1.txt file3.txt

You should see the following output:

Files file1.txt and file3.txt are identical

Find Differ Files Using Diff Command

You can use the -q flag with the diff command to check whether two files differ.

Run the following command to compare the file1.txt and file2.txt:

diff -q file1.txt file2.txt

You should see the following output:

Files file1.txt and file2.txt differ

Display Output in side-by-side View

If you compare two files and want to display file differences in a side-by-side view, use the -y option.

diff -y file1.txt file2.txt

You should see the following output:

11 12 13 14 15 16 17 18 19				      |	10 12 3 14 8 16 7 18 19
eveven twelve thirteen fourteen fifteen sixteen seventeen eig |	five twelve eight fourteen fifteen nine seventeen one ninteen
In this file, we added some numbers in text and numerix form	In this file, we added some numbers in text and numerix form
Atlantic Cloud							Atlantic Cloud

If you want to ignore the similarities between the two files from the above output, run the following command:

diff -y --suppress-common-lines file1.txt file2.txt

You should see the following output:

11 12 13 14 15 16 17 18 19				      |	10 12 3 14 8 16 7 18 19
eveven twelve thirteen fourteen fifteen sixteen seventeen eig |	five twelve eight fourteen fifteen nine seventeen one ninteen

Conclusion

In this post, we explained how to compare two files in Linux using the diff command. We also demonstrated several use cases to explain how to use the diff command in different conditions. Try it on VPS hosting from Atlantic.Net!

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year