How To Compare Two Files In Linux And Find The Differences

How to compare two files in Linux and find the differences. Diff is a Linux/Unix command to compare two files and show the differences on a Linux Ubuntu Systems.

In the simplest case, two file names FROM-FILE and TO-FILE are given, and `diff’ compares the contents of FROM-FILE and TO-FILE. A file name of `-‘ stands for text read from the standard input. As a special case, `diff – -‘ compares a copy of standard input to itself. If one file is a directory and the other is not, `diff’ compares the file in the directory whose name is that of the non-directory. The non-directory file must not be `-‘.

If two file names are given and both are directories, `diff’ compares corresponding files in both directories, in alphabetical order; this comparison is not recursive unless the `–recursive’ (`-r’) option is given. `diff’ never compares the actual contents of a directory as if it were a file. The file that is fully specified may not be standard input, because standard input is nameless and the notion of “file with the same name” does not apply.

Popular Command Options

  • `-q’ or `–brief’ : Report only whether the files differ, not the details of the differences.
  • `-r’ or `–recursive’ : When comparing directories, recursively compare any subdirectories found.
  • `-s’ or `–report-identical-files’ : Report when two files are the same.
  • `-y’ or `–side-by-side’ : Use the side by side output format.

How to Compare Two Files Using Linux

Create a file1 and file2. Assume the following contents in the respective files.

file1

Diff Command Example
Diff Command Example in Linux

file2
Diff Command Example
Diff Command Example in Linux
Diff Command Example in Unix

Ubuntu Diff Command Syntx and Example

Now let us compare the file1 and file2 using diff command

The command used within Linux to show the differences between 2 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 then there will be no output when using this command, however, as there are differences you will see output similar to the following:

Find If the Files Are Different

Use command option `-q’ or `–brief’ which reports only whether the files differ, not the details of the differences.

diff -q file1 file2

If the files are different the following message is displayed:

Files file1 and file2 differ

If the files are the same then nothing is shown.

Find If the Files Are Same

Use command option `-s’ or `–report-identical-files’ to report when two files are the same.

diff -s file1 file2

If the files are the same you will receive the following message on the screen:

Files file1 and file2 are identical

Display Difference On Screen

Use command option `-y’ or `–side-by-side’ to display the difference of two files in side by side output format.

diff -y file1 file2

How To Compare Two Files In Linux And Find The Differences originally posted on Source Digit – Latest Technology, Gadgets & Gizmos.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.