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...
Using Patches

When someone changes a file that other people have copies of (source code, documentation, or just about any other text file), they often send patches instead of (or in addition to) making the entire new file available. If you have the old file and the patches, you might wish that you could have a program apply the patches. You might think that normal diff format, which was made to look like input to the ed program, would be the best way to accomplish this.

As it turns out, this is not true.

A program called patch has been written which is specifically designed to apply patches to files (change the files as specified in the patch). It correctly recognizes all the formats of patches and applies them. With unified and context diffs, patch can usually apply patches, even if lines have been added or removed from the file, by looking for unchanged context lines. Only if the context lines have themselves been changed is patch likely to fail.

To apply patches with patch, you normally have a file containing the patch (we'll call it patchfile), and then run patch:

patch < patchfile

Patch is very verbose. If it gets confused by anything, it stops and asks you in English (it was written by a linguist, not a computer scientist) what you want to do. If you want to learn more about patch, the man page is unusually readable.

Other Related Tools

If you read the RCS article in the May issue (Take Command: Keeping Track of Change, LJ #25, May 1996), you may have noticed that the article talked a bit about a program called rcsdiff. rcsdiff is really just a front end to diff. That is, it looks for arguments that it understands (such as revision numbers and the filename) and prepares two files representing the two versions of the file you are examining. It then calls diff with the remaining options. The RCS article used -u to get the unified format without explaining what it meant, but you can use -c to get context diffs, or use -U lines to choose the amount of context you get in a unified diff, or use any other diff options you like.

You may notice that rcsdiff produces more verbose output than normal diff. From the RCS article:

rcsdiff -u -r1.3 -r1.6 foo
RCS file: foo,v
retrieving revision 1.3
retrieving revision 1.6
diff -u -r1.3 -r1.6
--- foo 1996/02/01 00:34:15     1.3
+++ foo 1996/02/01 01:05:28     1.6
 -1,2 +1,6
 This is a test of the emergency
-RCS system.  This is only a test.
+RCS version control system.
+This is only a test.
+I'm now adding a few lines for
+the next version.

It looks just like a normal unified diff except for the first 5 lines.

This doesn't prevent you from sending patches to people. The patch program is extremely good about ignoring extraneous information. It can even ignore news or mail headers, extra comments written in a file outside a patch, and people's signatures following patches. Patch tells you when it is determining whether text is part of a patch or not by saying “Hmm...

If you don't care how two files differ, but just want to know whether they differ, the cmp program will tell you. It works not only for text files, but also for binary files. In this example, the files 5 and 6 are different; 2 and 4 are the same:

cmp 5 6
5 6 differ: char 159, line 4
cmp 2 4

Notice that when two files are the same, cmp doesn't say anything at all. It only tells you explicitly if the files have been changed. For use in writing shell scripts, cmp also returns true if the files are the same and false if they don't, as shown by this shell session:

if cmp 5 6 ; then
  echo "same"
  echo "different"
5 6 differ: char 159, line 4
if cmp 2 4 ; then
  echo "same"
  echo "different"

There are several other programs with related functionality. In particular, diff3 can be used to merge together two different files that have both been edited from a common ancestor file. That common ancestor must exist in order for diff3 to work correctly.

The info pages which are shipped with diff are probably installed on your system. If you want to learn more about diff, try the command info diff or use info mode from within emacs or jed.

diff, wdiff, patch, and emacs are available via ftp from the canonical GNU ftp archive, prep.ai.mit.edu, in the directory /pub/gnu/

Michael K. Johnson His wife Kim likes A. A. Milne and briefly studied Latin (unlike Michael, whose experience with Latin was limited to singing in choir), which is why she owns Winnie Ille Pu as well as Tela Charlottae (Charlotte's Web).



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.


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*
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

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,

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:


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).


You didn't mention diff3 either, did you?