Linux DevCenter    
 Published on Linux DevCenter (
 See this if you're having trouble printing code examples

Unix Power Tools
patch: Generalized Updating of Files that Differ

by Tim O'Reilly

Like all of Larry Wall's widely used programs (including perl, a software configuration script called Configure, and the rn news reader), patch betrays a whimsical intelligence of its own. Feed it any kind of diff listing (not just an editing script produced with the - e option). patch figures out what it needs to do to apply the diff, and updates the file, supplying all the while a breezy commentary on what it's doing:

% patch < testfile.diff
Hmm...  Looks like a normal diff to me...
File to patch: testfile
Patching file testfile using Plan A...
Hunk #1 succeeded at 2.

As Larry once noted, patch has done an awful lot to "change the culture of computing." Almost all free software is now updated by means of patches rather than complete new releases. patch is smart enough to discard any leading or trailing garbage (such as mail headers or signatures) so that a program source file can be updated by piping a mail message containing a diff listing between old and new versions directly to patch.

Here are a few of the other things patch is smart enough to do:

If you are a programmer, patch is worth studying just to see how much a program can do to anticipate errors, deal with fuzzy input, and in general "make the computer do the dirty work." But if you're a programmer, you doubtless already know about patch....

Back More Unix Power Tools


Copyright © 2009 O'Reilly Media, Inc.