09. Finding Differences diff

A more powerful comparison with diff

We can use the diff command to compare files line by line with a richer set of output formats than we did with cmp.

Simply pass in the two file names as the arguments to the diff command.

$ cat file1.txt
Life goals 1) Graduate High School 2) Go skydiving 3) Help and feed the poor and hungry 4) Run a half marathon 5) Swim at the beach 6) Travel to Ireland 7) Get rock solid abs
$ cat file2.txt
Life bucket list 1) Graduate college 2) Go skydiving 3) Help and feed the poor 4) Run a marathon 5) Swim at the beach 6) Travel the world
$ diff file1.txt file2.txt
1,2c1,2 < Life goals < 1) Graduate High School --- > Life bucket list > 1) Graduate college 4,5c4,5 < 3) Help and feed the poor and hungry < 4) Run a half marathon --- > 3) Help and feed the poor > 4) Run a marathon 7,8c7 < 6) Travel to Ireland < 7) Get rock solid abs --- > 6) Travel the world

The output will tell you how to change the first file to get the second file. Lines beginning with a < mean that they're from the first file, while the > mean they're from the second. The --- signifies separation of file1.txt and file2.txt.

There are three letters that signify three types of changes:

a
add
c
change
d
delete

Thus in our example above, the diff command tells us to change lines 1,2 from the file1.txt to lines 1,2 in the second file. The same for lines 4 and 5 (line 3 requires no change). Then it tells us to change lines 7,8 to just line 7 of file2.txt.

Unified mode

With the -u option, we can avoid redundant information. Again, the output will show you how to go from the first argument file to the second.

Here are a list of symbols used in unified mode.

+
Lines that have been added.
-
Lines that have been deleted.
!
Lines that have been changed.
$ diff -u file1.txt file2.txt
--- file1.txt 2015-06-03 22:34:30.000000000 -0700 +++ file2.txt 2015-06-03 22:38:26.000000000 -0700 @@ -1,8 +1,8 @@ -Life goals -1) Graduate High School +Life bucket list +1) Graduate college 2) Go skydiving -3) Help and feed the poor and hungry -4) Run a half marathon +3) Help and feed the poor +4) Run a marathon 5) Swim at the beach -6) Travel to Ireland -7) Get rock solid abs +6) Travel the world +

Options

These a few common options used with the diff command. Make sure you check the man page for a complete listing.

-b
Ignore changes in white space.
-B
Ignore changes that insert or delete blank lines.
-i
Case insensitive.
-q
Report when two files are different.
-s
Report when two files are the same.
-w
Ignore white space.
-y
View side by side.

Aching back from coding all day?

Acupressure Mat & Pillow

Aching back from coding all day? Try Back Problems

Relieve your stress, back, neck and sciatic pain through 1,782 acupuncture points for immediate neck pain relief. Made for lower, upper and mid chronic back pain treatment, and improves circulation, sleep, digestion and quality of life.

$$ Check price
144.87144.87Amazon 4.5 logo(1,890+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

Command Line Kung Fu

Take your Linux skills to the next level! Try Linux & UNIX

Command Line Kung Fu is packed with dozens of tips and practical real-world examples. You won't find theoretical examples in this book. The examples demonstrate how to solve actual problems. The tactics are easy to find, too. Each chapter covers a specific topic and groups related tips and examples together.

$ Check price
14.9914.99Amazon 4.5 logo(27+ reviews)

More Linux & UNIX resources

Ad