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.

Man comparing two files on Linux terminal
Lifewire / Derek Abella

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:

  1. Open the file by typing the following command:

    нано файл1
    Terminal command: nano file1
  2. Enter text in nano editor .

    terminal file1 in nano
  3. Click CTRL+O to save the file.

  4. 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:

  1. Open the file by typing the following command:

    нано файл2
  2. Enter text into the nano editor.

    terminal file2 in nano
  3. Click CTRL+O to save the file.

  4. 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.

terminal command

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.

terminal command

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:

diff -Z file1 file2

How to ignore all white space differences between two files

If you are only interested in the text in the file and don’t care if there are more spaces in one place, use the following command:

diff -w file1 file2

How to ignore empty lines when comparing two files

If you don’t care about extra blank lines in the same file, compare the files with the following command:

diff -B file1 file2

Summary

You can find more information by reading diff command guide .

человек дифференциал

The diff command can be used in its simplest form to show only the differences between two files. You can also use it to create comparison file within correction strategies .

Another command that you can use to compare files is this is cmp command . This compares files byte by byte.

Похожие записи