Diff, Patch, and Friends

“Kernel patches” may sound like magic, but the two tools used to create and apply patches are simple and easy to use—if they weren't, some Linux developers would be too lazy to use them...
It's not always this easy

Unfortunately, if too many adjacent lines have been changed, interpretation isn't as immediately obvious; but by knowing that each marked line has been changed in some way, you can figure it out. For instance, in this comparison, where the file 3 contains the damaged contents, and file 4 (identical to file 2 in the previous example) contains the correct contents, three lines in a row are changed, and now each line with a difference is not shown directly above the corrected line:

diff -u 3 4
--- 3   Sun Apr 21 18:57:08 1996
+++ 4   Sun Apr 21 18:56:45 1996
 -1,9 +1,9
 Ecce Eduardus Ursus scalis nunc tump-tump-tump
 occipite gradus pulsante post Christophorum
 Robinum descendens. Est quod sciat unus et solus
-modus gradibus desendendi, non nunquam autem
-sentit, etiam alterum nodum exitare, dummodo
-pulsationibus desinere et de no modo meditari
+modus gradibus descendendi, nonnunquam autem
+sentit, etiam alterum modum exstare, dummodo
+pulsationibus desinere et de eo modo meditari
 possit. Deinde censet alios modos non esse. En,
 nunc ipse in imo est, vobis ostentari paratus.
 Winnie ille Pu.

It takes a little more work to find the added mistakes; “nodum” for “modum” and “exitare” for “exstare”. Imagine if 50 lines in a row had each had a one-character change, though. This begins to resemble the old job of going through the whole file, character-by-character, looking for changes. All we've done is (potentially) shrink the amount of comparison you have to do.

Fortunately, there are several tools for finding these kinds of differences more easily. GNU Emacs has “word diff” functionality. There is also a GNU “wdiff” program which helps you find these kinds of differences without using Emacs.

Let's look first at GNU Emacs. For this example, files 5 and 6 are exactly the same, respectively, as files 3 and 4 before. I bring up emacs under X (which provides me with colored text), and type:

M-x ediff-files RET
5 RET
6 RET

In the new window which pops up, I press the space bar, which tells Emacs to highlight the differences. Look at Figure 1 and see how easy it is to find each changed word.

Figure 1. ediff-files 5 6

GNU wdiff is also very useful, especially if you aren't running X. A pager (such as less) is all that is required—and that is only required for large differences. The exact same set of files (5 and 6), compared with the command wdiff -t 5 6, is shown in Figure 2.

Figure 2. wdiff -t 5 6

If you are getting extra character sequences like ESC[24 instead of getting underline and reverse video, it's probably because you are using less, which by default doesn't pass through all escape characters. Use less -r instead, or use the more pager. Either should work.

wdiff uses the termcap database (that's what the -t option is for) to find out how to enable underline and reverse video, and not all termcap entries are correct. In some instances, I've found that the linux termcap entry works well for other terminals, since the codes for turning underline and reverse video on and off don't differ very much across terminals. To use the linux termcap entry, you can do this:

TERM=linux wdiff -t 5 6 | less -r

This will work only with bourne shell derivatives such as bash, not with csh or tesh. But since you need to do this only to correct for a broken termcap database, this limitation shouldn't be too much of a problem.

wdiff isn't always built with the termcap support needed to underline and reverse video, and it's not always what you want even if you have a working termcap database, so there's an alternate output format that is just as easy to understand. We'll kill two birds with one stone by also showing off wdiff's ability to deal with re-wrapped paragraphs while showing off its ability to work without underline and reverse video. File 8 is the same as the correct file 2, shown at the beginning of this article, but file 7 (the corrupted one) now has much shorter lines, which makes them even harder to compare “by eye”:

Ecce Eduardus Ursus scalis
nunc tump-tump-tump occipite
gradus pulsante post
Christophorum Robinum
descendens. Est quod sciat
unus et solus modus gradibus
desendendi, non nunquam autem
sentit, etiam alterum nodum
exitare, dummodo pulsationibus
desinere et de no modo
meditari possit. Deinde censet
alios modos non esse. En, nunc
ipse in imo est, vobis
ostentari paratus.
Winnie ille Pu.

wdiff is not confused by the differently-wrapped lines. The command wdiff 7 8 produces this output:

Ecce Eduardus Ursus scalis nunc tump-tump-tump
occipite gradus pulsante post Christophorum
Robinum descendens. Est quod sciat unus et solus
modus gradibus
[-desendendi, non nunquam-]
{+descendendi, nonnunquam+} autem
sentit, etiam alterum [-nodum
exitare,-] {+modum exstare,+} dummodo
pulsationibus desinere et de [-no-] {+eo+}
modo meditari
possit. Deinde censet alios modos non esse. En,
nunc ipse in imo est, vobis ostentari paratus.
Winnie ille Pu.

Remember the + and - characters? They mean the same thing with wdiff as they mean with diff. (Consistent user interfaces are wonderful.)

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

The first comment asked for

Anonymous's picture

The first comment asked for reasons. Here are a few.

1. Years of fighting with graphical tools can lead one to learn command line tools.

2. A major difference between the two is the former can be used in scripts.

3. Another significant difference is robustness. It's generally much easier to crash a graphical tool than a well-designed command line one.

4. Lastly, command line tools most often contain less code and require less system resources to operate, often making them better suited to work faster and more efficiently than graphical tools.

Smaller, faster and more reliable. Built for automation. A higher learning curve may be a trade off for higher performance.

Why?

Jonathan Allen's picture

Why in the world would I want to fight with command line tools when trying to compare and merge versions? Graphical diff and merge tools have existed for decades.

Now if you can show me a diff and merge tool that understands the syntax of the file being compared and I'll be far more interested. I have seen merge tools eat a brace way too often.

I think the images for figure

Anonymous's picture

I think the images for figures 1 and 2 are broken. When I follow the links to view these figures, the images don't show up in my browser window.

using diff and patch

John's picture

If I use diff -Naur to generate a patch symbolic links are not respected. e.g. see below;

How can I get patch/diff to respect symbolic links?

>---------------------------------------------------<
$ ll foo*
foo:
total 4
lrwxrwxrwx 1 foo users 5 Jun 9 13:04 link -> stuff
-rw-r--r-- 1 foo users 12 Jun 9 13:04 stuff

foo2:
total 4
lrwxrwxrwx 1 foo users 5 Jun 9 13:04 link -> stuff
-rw-r--r-- 1 foo users 12 Jun 9 13:04 stuff

$ diff -Naur foo foo2 > patch
$ mv foo2 foo2.orig
$ patch -p0 < patch
patching file foo/link
patching file foo/stuff

$ ll foo
total 8
-rw-r--r-- 1 foo users 27 Jun 9 13:08 link
-rw-r--r-- 1 foo users 27 Jun 9 13:08 stuff

>---------------------------------------------------<

Working with directories

Anonymous's picture

This article is missing info on patching multiple files.

See here: http://lists.gnu.org/archive/html/help-gnu-utils/2004-06/msg00024.html for examlpe.

Re: Diff, Patch, and Friends

Anonymous's picture

Nice article! I now link to it from
my "Howto contribute to an open source project" tutorial,
www.kegel.com/academy/opensource.html

Re: Diff, Patch, and Friends

Anonymous's picture

You may want to link to the manpages for the free versions
of diff and patch too, instead of only the GNU versions:

http://mirbsd.bsdadvocacy.org/man1/diff.htm
http://mirbsd.bsdadvocacy.org/man1/patch.htm

To the editor: ed(1) is by no means obsolete; I'm actually
faster with ed than with vi (whose modus operandi is
cruelly to a wordstar-compatible editor user like me).

http://mirbsd.bsdadvocacy.org/man1/ed.htm

You didn't mention diff3 either, did you?

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix