Cyberithub

15 Popular diff command examples in Linux(Compare two files)

Advertisements

In this article, we will go through 15 Popular diff command examples in Linux. diff is a part of GNU Project and is freely available on Linux platform. It is a very popular tool in Linux world which is used to compare and find the difference between two files or two directories. You might wonder how this command really works ? diff utility takes two files as input and compare both of them line by line and show you the list of lines which are not same. Similarly, in case of directory, it will show the list of files which are not same on both the directories. We will understand more about this tool using some real world examples in below section.

But before that Let's understand all the commands used by diff utility to indicate the difference between two files.

Advertisements
  • a: this command indicates that something has been added.
  • c: this command indicates that some text has been changed.
  • d: this command indicates some text has been deleted.

Synopsis

diff [OPTION]... FILES

15 Popular diff command examples in Linux(Compare two files)

diff command examples in Linux

Also Read: Step by Step Guide to Install Slack Desktop on Ubuntu 20.04

Advertisements

Example 1: How to Check diff command version

If you want to check diff command version then you need to run diff -version command as shown below. It will show the GNU diffutils package version currently installed. Here it is version 3.3.

[root@localhost ~]# diff -version
diff (GNU diffutils) 3.3
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Paul Eggert, Mike Haertel, David Hayes,
Richard Stallman, and Len Tower.

Example 2: How to Compare two files using diff command

Here we have created two files file1.txt and file2.txt for our demonstration purpose. Both of the file contains two lines as shown below. If you now go ahead and compare both the files using diff file1.txt file2.txt command then you will see the difference on the output.

Advertisements
[root@localhost ~]# cat file1.txt
Hi This is from Cyberithub
This is an example file
[root@localhost ~]# cat file2.txt
Hello This is from Cyberithub
This is an example file
[root@localhost ~]# diff file1.txt file2.txt
1c1
< Hi This is from Cyberithub
---
> Hello This is from Cyberithub

Example 3: How to Compare and show output in Context Format

If you want to check the difference between two files in context format then you need to use -c option with diff command. The context output format shows several lines of context around the lines that differ. It is the standard format for distributing updates to source code.

[root@localhost ~]# diff -c file1.txt file2.txt
*** file1.txt 2021-05-10 08:49:00.228137256 -0400
--- file2.txt 2021-05-10 08:49:18.004048256 -0400
***************
*** 1,2 ****
! Hi This is from Cyberithub
This is an example file
--- 1,2 ----
! Hello This is from Cyberithub
This is an example file

-c : output NUM (default 3) lines of copied context. More on Man Page.

Advertisements

Example 4: How to Compare and show output in Unified Format

If you want to see the compared output in unified format then you need to use -u option as shown below.

[root@localhost ~]# diff -u file1.txt file2.txt
--- file1.txt 2021-05-10 08:49:00.228137256 -0400
+++ file2.txt 2021-05-10 08:49:18.004048256 -0400
@@ -1,2 +1,2 @@
-Hi This is from Cyberithub
+Hello This is from Cyberithub
This is an example file

-u: output NUM (default 3) lines of unified context.

Example 5: How to query and report only when files differ

If you want diff command to report only if both the files are different or not, then -q option can be used with diff command.

[root@localhost ~]# diff -q file1.txt file2.txt
Files file1.txt and file2.txt differ

-q: report only when files differ.

Example 6: How to Compare and show ed script

If you want to compare two files and show the output in ed script format then you need to use -e option with diff command. Long ago, this was the only output mode in use. Today with patch, this option has almost became obsolete.

[root@localhost ~]# diff -e file1.txt file2.txt
1c
Hello This is from Cyberithub
.

-e: output an ed script.

Example 7: How to Compare and output in two columns

If you want to show the compared output in two columns then you need to use -y option as shown below.

[root@localhost ~]# diff -y file1.txt file2.txt
Hi This is from Cyberithub | Hello This is from Cyberithub
This is an example file This is an example file

-y : output in two columns.

Example 8: How to Compare and Show output in RCS Format

If you want to compare two files and check the output in RCS format then you need to use -n option with diff command as shown below. RCS format is specifically designed to use by Revision Control System, which is a set of free programs used for organizing different versions and set of files. This format uses the combination of a and d commands on the output instead of c.

[root@localhost ~]# diff -n file1.txt file2.txt
d1 1
a1 1
Hello This is from Cyberithub

-n : output an RCS format diff.

Example 9: How to Ignore Case Sensitive while comparing files

If you want to ignore case sensitivity while comparing two files then you need to use -i option with diff command as shown below.

[root@localhost ~]# diff -i file1.txt file2.txt
1c1
< Hi This is from Cyberithub
---
> Hello This is from Cyberithub

-i : ignore case differences in file contents.

Example 10: How to Ignore white space while comparing files

If you want to ignore white space while comparing two files then you need to use -b option with diff command as shown below.

[root@localhost ~]# diff -b file1.txt file2.txt
1c1
< Hi This is from Cyberithub
---
> Hello This is from Cyberithub

-b : ignore changes in the amount of white space.

Example 11: How to Check all the available options of diff command

If you want to check all the options available with diff command then you need to use --help option with diff command as shown below.

[root@localhost ~]# diff --help
Usage: diff [OPTION]... FILES
Compare FILES line by line.

Mandatory arguments to long options are mandatory for short options too.
--normal output a normal diff (the default)
-q, --brief report only when files differ
-s, --report-identical-files report when two files are the same
-c, -C NUM, --context[=NUM] output NUM (default 3) lines of copied context
-u, -U NUM, --unified[=NUM] output NUM (default 3) lines of unified context
-e, --ed output an ed script
-n, --rcs output an RCS format diff
-y, --side-by-side output in two columns

Example 12: How to Compare two directories using diff command

Likes files, you can also compare two directories. Here we have two directories example and test created where contents of both the directories are different. Now if you want both of these directories to be compared for any difference then you need to use diff example/ test/ command as shown below.

[root@localhost ~]# diff example/ test/
Only in test/: c_program
Only in example/: hello.txt
Only in test/: python
Only in example/: test

Example 13: How to redirect diff command output to a file

You can also the redirect the diff command output to a file instead of displaying it on the output. You just need to use the redirection operator to redirect output. Here we are comparing file1.txt and file2.txt files and redirecting the compared output to file.txt using diff file1.txt file2.txt > file.txt command as shown below.

[root@localhost ~]# diff file1.txt file2.txt > file.txt
[root@localhost ~]# cat file.txt
1c1
< Hi This is from Cyberithub
---
> Hello This is from Cyberithub

Example 14: How to treat all files as text file

If you want to treat all the comparing files as text file then you need to use -a option with diff command as shown below.

[root@localhost ~]# diff -a file1.txt file2.txt
1c1
< Hi This is from Cyberithub
---
> Hello This is from Cyberithub

-a : treat all files as text.

Example 15: How to Check Man Page of diff command

If you want to check the man page of diff command then you need to use man diff command as shown below.

[root@localhost ~]# man diff
DIFF(1) User Commands DIFF(1)

NAME
diff - compare files line by line

SYNOPSIS
diff [OPTION]... FILES

DESCRIPTION
Compare FILES line by line.

Mandatory arguments to long options are mandatory for short options too.

--normal
output a normal diff (the default)

-q, --brief
report only when files differ

-s, --report-identical-files
report when two files are the same

-c, -C NUM, --context[=NUM]
output NUM (default 3) lines of copied context

-u, -U NUM, --unified[=NUM]
output NUM (default 3) lines of unified context

Leave a Comment