9 ‘diff’ Command Examples in Linux

Diff command in Linux is used to compare the content of two files line by line and if the difference is found then it will also list differences along with line numbers. Diff command can also be used to compare the contents of two directories.

Syntax of diff command:

# diff <options> file1 file2

Output of diff command can be in following format:

  • Normal (default)
  • Context
  • Unified

Symbols used in diff command output are:

  • a -> it indicates that something has been added
  • c -> it indicates that some text has been changed
  • d -> it indicates some text has been deleted

By default, diff command output is in normal format, it means when contents of two files are identical then it will not produce any output but we will get the prompt.

In this article, we will learn Linux diff command with different practical examples,

Example:1) Compare two files with diff command

Let’s assume we have two files aachen.txt and sydney.txt and following are contents of these files,

[[email protected] ~]# cat aachen.txt
Linux Rocks in the open source world, followings are the linux distributions:
CentOS
Red Hat
Ubuntu Debian
Linux Mint
OpenSUSE
Fedora
[[email protected] ~]# cat sydney.txt
Linux Rocks in the open source world, followings are the linux distributions:
CentOS
Red Hat
Ubuntu Debian
Linux Mint
OpenSUSE
Fedora
[[email protected] ~]#

Let’s compare the content of these files using diff command,

[[email protected] ~]# diff aachen.txt sydney.txt
[[email protected] ~]#

Above output confirms that both the files are identical. Let’s make some changes in aachen.txt, re-write open source as “open-source”.

Now re-run the diff command,

[[email protected] ~]# diff aachen.txt sydney.txt

Output,

Change-diff-command-output

In the output, 1c1 indicates that 1st line of the first file needs to be changed to make both the files identical.

Let’s roll-back the above change and add a new line “Arch Linux” in sydeny.txt at end of file and run the diff command again,

add-diff-command-output

Output of diff command “7a8” indicates that after the 7th line in the first file, we need to add another line to match 8th line of the second file.

Let’s remove “Arch Linux” and “Fedora” from sydney.txt and then try to compare these files,

delete-text-diff-command-output

Output of diff command “7d6” indicates that we need to delete 7th line in the first file to sync with the second file at line number 6.

Example:2) Diff command output in context format (-c)

Use “-c” option in diff command to produce the diff command output in context format. Example is shown below,

[[email protected] ~]# diff -c aachen.txt sydney.txt

diff-command-output-context-format

First two lines represent file names along with their modification date and time and three asterisk symbol (“***“) indicates the first file and three hyphen symbol (““) indicates the second file.

Only single hyphen “-” indicates that line needs to removed and plus symbol “+” indicates that line needs to added in the file. If any line which doesn’t require any change then it is prefixed by two spaces.

Example:3) Diff command output in unified format (-u)

Use “-u” option in diff command if you want to produce its output in unified format, example is illustrated below,

[[email protected] ~]# diff -u aachen.txt sydney.txt

diff-command-output-unified-format

Above output is somewhat similar to context format but unified format displays the output in concise way, here first two lines indicates the file names along with their modification date and time..

Three hyphens (“—“) represents the first file and three plus symbol (“+++”) represents second file, after two @ sign “-1,7” indicates the lines from 1st file and “+1,7” represents lines range from 2nd file. Plus, symbol (+Core OS) indicates that this line needs to be added in first file and symbol hyphen (-Fedora) indicates that this line needs to be removed from first file to make it identical to 2nd file.

Example:4) Ignore Case Sensitive while Comparing  files (-i)

By default, diff command is case sensitive and if you want to ignore case sensitive in diff command then use “-i” option, example is shown below,

Let’s assume we have the following content of two files

Content-two-files

Run the diff command without any option,

[[email protected] ~]# diff aachen.txt sydney.txt
5,6c5,6
< Ubuntu debian
< Linux mint
---
> Ubuntu Debian
> Linux Mint
[[email protected] ~]#

Now run the diff command with -i option,

[[email protected] ~]# diff -i aachen.txt sydney.txt

[[email protected] ~]#

As we can see in above output, diff command has ignored case sensitive feature when we use -i option.

Example:5) Report files are identical with diff command (-s)

There can be some situations where we are comparing the files using diff command and if content is identical then we want display the message that file are identical, this can be accomplished by passing “-s” option in diff command,

[[email protected] ~]# cat filex.txt
DevOPs Engineer
Cloud Stack Engineer
Linux System Admin
Monitoring Team
[[email protected] ~]# cat filey.txt
DevOPs Engineer
Cloud Stack Engineer
Linux System Admin
Monitoring Team
[[email protected] ~]# diff -s filex.txt filey.txt
Files filex.txt and filey.txt are identical
[[email protected] ~]#

Example:6) Ignore White Space While Comparing files with diff  (-b)

Let’s assume we have two files filea.txt and fileb.txt where white spaces are not consistent, so if we use diff command without any option in that it will show difference of white space even though contents are identical, so in such cases we can ignore inconsistent white spaces in diff command by using -b option , example is shown below,

[[email protected] ~]# cat filea.txt
Hi,  LinuxTechi is a Linux based  blog. It contains linux  HowTo's  and  Tips  &  Tricks.
[[email protected] ~]# cat fileb.txt
Hi, LinuxTechi is a Linux based blog. It contains linux HowTo's and Tips & Tricks.
[[email protected] ~]#

Compare the files without any option with diff command,

[[email protected] ~]# diff filea.txt fileb.txt
1c1
< Hi,  LinuxTechi is a Linux based  blog. It contains linux  HowTo's  and  Tips  &  Tricks.
---
> Hi, LinuxTechi is a Linux based blog. It contains linux HowTo's and Tips & Tricks.
[[email protected] ~]#

Now use -b option in above diff command

[[email protected] ~]# diff -b filea.txt fileb.txt
[[email protected] ~]#

If you want to ignore all the white spaces during comparison using diff command, then use “-w” option

[[email protected] ~]# diff -w filea.txt fileb.txt

Example:7) Ignore White Space at line end during comparison (-Z)

If you want to Ignore trialing space at the line end during the comparison, then use “-Z” option

[[email protected] ~]# diff -Z filea.txt fileb.txt

Use “-B” in diff command to ignore all blank lines during the comparison,

[[email protected] ~]# diff -B filea.txt fileb.txt

Example:8) Compare two directories with diff command

Let’s assume we have two directories with the name lab01 and lab02, these have following files and sub-directories

[[email protected] ~]# ls -l lab01/
total 0
-rw-r--r--. 1 root root  0 Feb  1 17:49 filea.txt
-rw-r--r--. 1 root root  0 Feb  1 17:49 fileb.txt
-rw-r--r--. 1 root root  0 Feb  1 17:49 filec.txt
-rw-r--r--. 1 root root  0 Feb  1 17:49 filed.txt
drwxr-xr-x. 2 root root 23 Feb  1 17:50 sitea
[[email protected] ~]# ls -l lab02/
total 0
-rw-r--r--. 1 root root  0 Feb  1 17:49 filea.txt
-rw-r--r--. 1 root root  0 Feb  1 17:49 fileb.txt
-rw-r--r--. 1 root root  0 Feb  1 17:49 filec.txt
-rw-r--r--. 1 root root  0 Feb  1 17:49 filed.txt
drwxr-xr-x. 2 root root 23 Feb  1 17:50 sitea
[[email protected] ~]#

Let’s compare these two directories using below diff command,

[[email protected] ~]# diff -qr lab01/ lab02/
[[email protected] ~]#

Where:

  • -q -> it instructs diff command to report only when files are different
  • -r -> it instructs diff command to look for difference in sub-directories recursively

As we get blank output, it means both the directories are identical. Let’s delete filed.txt from lab02 and then try to compare,

[[email protected] ~]# cd lab02/
[[email protected] lab02]# rm -f filed.txt
[[email protected] lab02]# cd
[[email protected] ~]# diff -qr lab01/ lab02/
Only in lab01/: filed.txt
[[email protected] ~]#

Above output says that filed.txt is only present in lab01 directory

Example:9) Redirecting diff command output to a file

Diff command output can be redirected to a file by using symbol “>”. It becomes very useful in shell scripts where we want to perform a task only if output of diff command contains differences.

[[email protected] ~]# diff aachen.txt sydney.txt
9,10d8
< SLES
< Puppy Linux
[[email protected] ~]# diff aachen.txt sydney.txt  > diff.txt
[[email protected] ~]# cat diff.txt
9,10d8
< SLES
< Puppy Linux
[[email protected] ~]#

That’s all from this article, I hope now you have better understanding of diff command by going through above examples. Please do not hesitate to share your feedback and comments.

1 Response

  1. jenglish says:

    A favorite of mine is ‘diff -y’ – it shows the two file outputs side by side in the terminal.

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest