• 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

Ubuntu 25.04 ‘Plucky Puffin’ Is Available To Download

How to Login as Root User in Linux Terminal

Install Firefox In Ubuntu Terminal Command Line

How To Check Kernel Installed Date In Linux Ubuntu

How To List All Kernel Modules That Are Loaded In Ubuntu

Enable Ubuntu Auto Login Without Password

Download Ubuntu 25.04 Official Default Wallpapers

How To Fix Initramfs Error In Ubuntu Command Line

How To Check All Available Kernel Version In Linux Ubuntu

3 Best Artificial Intelligence Domain Name Generators

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

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