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.

Take your Linux skills to the next level!

The Linux Command Line

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

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Inversion Therapy Table

Aching back from coding all day? Try Back Problems

Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.

$$ Check price
119.98119.98Amazon 4.5 logo(1,700+ reviews)

More Back Problems resources

Ad