June 7

Linux: Using diff to compare files


diff reports the differences between two files.

diff [options] <i>file1</i> <i>file2</i>

Simple Usage Example

File test1:

A common line.
Really common line.
One more common line.
Only here.

File test2:

A almost common line.
Really common line.
Really not common line.
One further line.
One more common line.

For example

$ diff test1 test2

will output:

< A common line.

> A almost common line.
> Really not common line.
> One further line.
< Only here.

What does that mean? First of all this output can be used as a script for ed (see example ). Lines
from first file are preceded by a less then symbol ( < ) and lines from
second file by a greater then symbol ( > ).
A dashed line ( — ) is used to separate output from the two files.
The letters can be used to convert file1 into file2:
c Replace lines from file1 with those from file2.
d Delete lines from file1.
a Add lines from file2 to file1.

The two files have three differences:

Line 2 differs.
Lines 4 and 5 of test2 are not present in test1. These lines would need to
be added after line 3 of test1 for the files to be the same.

Since you normally think in terms of converting the first file into the second file it is better to say that line 5 would need to be deleted from test1 for the files to be the same.

Useful Options



Ignores repeated blanks (e.g. <space><space> is the same as <space>) and blanks at the end of lines.

Ignore all spaces and tabs (e.g. 1 or 2 is equivalent to 1or2).

Ignore case (e.g. howdy, HOWDY and HoWdY are equivalent).

Use the context output format. Context output includes three lines before and after those that are normally printed to give “context” for the differences.

Use the unified output format, easy readable with file information.

-C n
Like -c but include n lines of context output.

Produce a script file that can be used by ed to convert file1 to file2. This option is not used as often as the patch command to convert file1 to file2.

Do a faster but less accurate comparison. This does not work well if the files are very different and cannot be used with the -e option.

Directory related Options



Output is formatted so that each file comparison occurs on a new page. Other comparisons are listed on a final page.

Recursively compare all files in common subdirectories.

Include a listing of all identical files in the output.


Comparing Directories

diff /tmp/oldFolder/ /tmp/newfolder/

Try it out! The output is self-explaining.

Changed files in folder tree

lists all files that have changed in a folder tree

diff -uwrq /tmp/oldFolder/ /tmp/newfolder/

Ignore Case and Repeated Blanks

Report the differences between poem1 and poem2 using the -i option to ignore the differences between upper and lower case charactersand the -b
option which ignores all repeated blanks and blanks at the end of lines.

diff -ib test1 test2

Patch with Diff

The recommended way …
Using output redirection

diff test1 test2 > diff_for_patch

patch test1 diff_for_patch

test1 is converted into test2 regarding the diff results, wich you can
influence by using options.


diff -r dir1 dir2 > dir2.patch

you can create a patch over all files in a directory which you can apply using

cd dir1 ; patch -p1 < dir2.patch

Ed with Diff

The -e option creates a script that gives directives to the ed text editor to convert file1 into file2.

diff -e test1 test2 > diff_for_ed

( cat diff_for_ed && echo w )| ed – test1

( ) for subshells, | – piping, ‘echo w’ is appended to ed input to make ed write the file

Diff with Context and more verbose Output

diff -c test1 test2

You can use the -C n option for n context lines. Output related to file1 is
preceded by stars (***) and file2 by dashes (—). Differences are separated by a long row of stars (***************).
In output the following symbols are used:

! Indicates corresponding lines in the two files that differ.
+ Indicates lines that exist in file2 but not file1.
– Indicates lines that exist in file1 but not file2.

Diff with Script

Using the Bourne shell:

# use -h option for faster, less accurate comparison
diff -h $1 $2 &amp;gt; /dev/null
# Asking for exit status using $? for bourne shell, 0 means equal
if [ $? -eq 0 ]; then
echo identical
elif [ $? -eq 1 ]; then
echo different
echo an error occurred

By: K Rekk

Copyright 2021. All rights reserved.

Posted June 7, 2012 by Timothy Conrad in category "Linux

About the Author

If I were to describe myself with one word it would be, creative. I am interested in almost everything which keeps me rather busy. Here you will find some of my technical musings. Securely email me using - PGP: 4CB8 91EB 0C0A A530 3BE9 6D76 B076 96F1 6135 0A1B