• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Source Digit

Source Digit

Linux, Ubuntu Tutorials & News, Technology, Gadgets & Gizmos

  • Home
  • How To
  • Ubuntu
  • Instagram
  • Blogging
  • Android
  • WordPress
  • +More
    • Cloud Computing
    • Gadgets
    • Social Media
    • Technology
  • Write for Us
Home / How To / How To Compare Two Files In Linux And Find The Differences

How To Compare Two Files In Linux And Find The Differences

By Sourabh / August 27, 2019 Category: How To, Ubuntu

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

Tags: Cmp Command In Linux, Comm Command In Linux, Compare Two Files In Linux And Find The Differences, Diff Command Example, Diff Command Example in Linux, Diff Command Example in Unix, Diff Command Output, Linux Diff Directories, Linux Diff Gui, Linux Diff Two Files, Unix Command To Compare Two Files And Show The Differences

Primary Sidebar

Latest Posts

Use Locate Command To Find File In Linux Terminal By Name

How To Change Terminal Color In Ubuntu Command Line

How To Run Fsck Manually In Linux To Fix Unexpected Inconsistency

How To Fix Broken Packages On Ubuntu From Terminal

Re Execute The Previous Command In Linux Ubuntu Command Line

How Much RAM Is Needed For Ubuntu PC

How To Find Large Files And Directories In Linux Command Line

How To Use Clear Command History In Linux Terminal

Linux Command To Check RAM Size In Ubuntu Terminal In GB

How To Install Proprietary Drivers In Linux Ubuntu Using Terminal

© 2026 Source Digit • Linux, Ubuntu Tutorials & News, Technology, Gadgets & Gizmos

  • Home
  • About
  • Privacy Policy
  • Terms & Conditions
  • Contact Us
  • Sitemap