This tutorial shows you how to use Linux to compare two files and print the differences between the files to the screen or to a file. You won’t be installing special file comparison software with Linux, but you need to know how to open a terminal window .
The fastest way to open a terminal window in Linux is to press CTRL+ALT+T keys at the same time.

Create files for comparison
If you want to follow this tutorial, create two text files that contain similar but different text.
Create first file
Create a file named file1 and enter the following text:
10 зеленых бутылок на стене
10 зеленых бутылок на стене
Если одна зеленая бутылка случайно упадет
На стене будет 9 зеленых бутылок
To create this file, follow these instructions:
-
Open the file by typing the following command:
нано файл1
-
Enter text in nano editor .
-
Click CTRL+O to save the file.
-
Click CTRL+X to exit the file.
Create a second file
Then create another file named file2 and enter the following text:
10 зеленых бутылок, стоящих на стене.
Если одна зеленая бутылка случайно упадет.
На стене будет 9 зеленых бутылок.
To create this file, follow these instructions:
-
Open the file by typing the following command:
нано файл2
-
Enter text into the nano editor.
-
Click CTRL+O to save the file.
-
Click CTRL+X to exit the file.
How to compare two files using Linux
The command used in Linux to show differences between two files is called the diff command.
The simplest form of the diff command is as follows:
diff file1 file2
If the files are the same, no output is displayed when using this command. However, since there are differences, the output is similar to the following:
2,4c2,3
<10 зеленых бутылок, стоящих на стене
<Если одна зеленая бутылка случайно упадет
<Там будет 9 зеленых бутылок, стоящих на стене
...
> Если одна зеленая бутылка случайно упадет
> Там будет 9 зеленых бутылки стоят на стене
Initially, the output seems confusing, but once you understand the terminology, it becomes quite logical.

The differences between these two files are as follows:
- The second file has only three lines. The first file has four.
- The second file says 1 green bottle in the third line. The first file says one green bottle .
- The second file says there would instead of there was would to the last line.
The output of the diff command shows that there are differences between lines 2 and 4 of the first file and lines 2 and 3 of the second file.
It then lists lines two through four from the first file, followed by two different lines in the second file.
How to show only if files are different
If you only want to know if the files are different and not interested in which lines are different, run the following command:
diff -q file1 file2
If the files are different, the following is displayed:
Файлы file1 и file2 отличаются
If the files are the same, nothing is displayed.
How to show a message if the files are the same
When you run a command, you can know that it is working correctly. Do you want a message to be displayed when you run the diff command whether the files are the same or different
To fulfill this requirement with the diff command, use the following command:
diff -s file1 file2
If the files match, this message appears:
Файлы file1 и file2 идентичны
How to make differences side by side
If there are multiple differences, it can lead to confusion as to what the differences are between the two files. You can change the output of the diff command so that the results are displayed side by side. To do this, run the following command:
diff -y file1 file2
The output for the file uses | a character to show the difference between two strings, <, to show the deleted line, and >, to show the added line.

When you run the command using the demo files in this article, all lines appear as different except for the last line file2 , which is displayed as deleted.
Limit column width
When comparing two files side by side, it can be difficult to read if the files have multiple columns of text. To limit the number of columns, use the following command:
diff --width = 5 файл1 файл2
How to ignore case differences when comparing files
If you want to compare two files but don’t care if the letter case matches between the two files, use the following command:
diff -i file1 file2
How to ignore trailing space at the end of a string
If you notice a lot of differences when comparing files and the differences are caused by spaces at the end of lines, prevent them from showing up as changes by running the following command: