UNIX in a Nutshell: System V Edition

UNIX in a Nutshell: System V EditionSearch this book
Previous: Reference: pasteChapter 2
Unix Commands
Next: Reference: pathchk
 

patch

patch [options] [sourcefile [patchfile]]

Solaris only. patch reads a "patch" containing the output of diff in normal, ed-script, or context format, and applies the changes contained therein to the original version of sourcefile. Multiple files can be patched, but it must be possible to determine the name of the original file from the contents of the patch. Distributing patches is an easy way to provide upgrades to source file distributions where the changes are small relative to the size of the entire distribution.

Note: this entry documents the Solaris version, which is a somewhat older version of Larry Wall's original patch program. The Free Software Foundation now maintains patch. Newer, more capable versions are available from them and are recommended; see http://www.gnu.org.

Options

-b

Make a backup of each file, in file.orig. An existing file.orig is overwritten.

-c

The patchfile is a context diff (from diff -c or diff -C).

-d dir

Change directory to dir before applying the patch.

-D identifier

Bracket changes with C preprocessor #ifdef.

#ifdef identifier
   ...
#endif

-e

The patchfile is an ed script (from diff -e).

-i file

Read the patch from file instead of from standard input.

-l

Patch loosely. Any sequence of whitespace characters in the patch may match any sequence of whitespace in sourcefile. Other characters must match exactly.

-n

The patchfile is a normal diff (from diff with no special options).

-N

Ignore patches that have already been applied. Normally, such patches are rejected.

-o newfile

Instead of updating each source file in place, write the full contents of the modified file(s) to newfile. If a file is updated multiple times, newfile will contain a copy of each intermediate version.

-pN

Remove N leading pathname components from the filename used in the patch. The leading / of a full pathname counts as one component. Without this option, only the final filename part of the filename is used.

-r rejfile

Use rejfile to contain patches that could not be applied, instead of file.rej. Rejected patches are always in context diff format.

-R

Reverse the sense of the patch. In other words, assume that the patch was created using diff new old, instead of diff old new.

Example

Update a software distribution:

$ cd whizprog-1.1
$ patch -p1 < whizprog-1.1-1.2.diff
Lots of messages here as patch works
$ find . -name '*.orig' -print | xargs rm
$ cd ..
$ mv whizprog-1.1 whizprog-1.2


Previous: Reference: pasteUNIX in a Nutshell: System V EditionNext: Reference: pathchk
Reference: pasteBook IndexReference: pathchk

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System