Unix Power ToolsUnix Power ToolsSearch this book

20.9. patch: Generalized Updating of Files That Differ

Figure Go to http://examples.oreilly.com/upt3 for more information on: patch

Like all of Larry Wall's widely used programs (including perl (Section 41.1), 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 (Section 11.1) (not just an editing script produced with the -e option -- the typical way before patch came around). 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.
done

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

One last note: patch is so useful that it's been added to many Unix systems. Check to see if your system has it before installing the program. Some versions of patch we've seen are limted versions or buggy when they come from software vendors, though. The one on the book's website is worth comparing to yours.

-- TOR



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.