patch will take a patch file containing any of the three forms of difference listing (normal, with context or in the ed format), produced by the diff (1 program and apply those differences to an original file, producing a patched version. By default, the patched version is put in the place of the original. If you specify the -b option, the original file is stored under the same name with the extension ".orig".
You may also specify where you want the output to go with a -o option.
Upon startup, patch will attempt to determine the type of the diff listing, unless overruled by a -c, -e or -n option. Context diffs and normal diffs are applied by the patch program itself, while -e diffs are simply fed to the ed editor via a pipe.
patch will try to skip any leading extraneous material, apply the diff, and then skip any trailing extraneous material. If the entire diff is indented by a consistent amount, this will be taken into account.
Syntax
patch[ -blNR][ -c| -e| -n][ -d dir][ -D define][ -i patchfile][ -o outfile] [ -p num][ -r rejectfile][file] |
options
saves all original files with a .orig suffix before the patch is applied. The backup file is overwritten if it already exists; if patch is executed several times, a backup file is only created on the first execution. The additional option -o does not save the original file, but saves the output file if it already exists.
Interprets the patch file as a context diff (output from diff, if -c or -C is specified).
patch changes to the dir directory before further actions take place.
marks changes with the instruction
Note that, unlike with the C compiler, there must be a space between the -D option and the argument.
Interprets the patch file as an ed script.
patch reads the patch from the patchfile file. If you enter a dash for patchfile, patch reads from the standard input. A patch file contains one or more patches and maybe some additional information. If a patch file contains several patches, each patch should contain information about filenames (like with diff -c), so that patch can find affected files automatically. patch evaluates the following information:
pathname names the file to be corrected
pathname specifies the "old" file on which the patch is based
pathname specifies the file to be corrected (has priority over Index:) Each patch contains patch instructions, which correspond to one of the three kinds of diff. -i not specified: patch reads from the standard input.
Any sequence of tabs and blanks in the patch file will match any sequence of tabs and blanks in the input file. However, normal characters must still match exactly.
Interprets the patch file as a normal diff.
Ignores patches that have already been applied. Such patch instructions are rejected by default.
The corrected version is written to outfile.Several corrected files are appended one after the other. If different patches affect the same original file, the following scripts are applied to a temporary file.
Controls the handling of pathnames found in the patch file. num specifies how many slashes are to be stripped from the front of the pathname. (Any intervening directory names are also removed.) For relative pathnames, the search takes place in the current directory or in the directory specified by the -d option. -p not specified: Example Supposing the filename in the patch file was
Setting the -p
without the leading slash, the -p
and not specifying the -p option at all just gives you
swaps the patch instructions before they are applied to the original file. This is necessary if the old and new files were exchanged when creating the patch. patch attempts to swap each hunk before it is applied. Rejects are written to the error file in If the first hunk of a patch fails, patch swaps the patch instructions to see if they can be applied in this way. If this is possible, you are asked whether the -R option should be set. If this is not possible, patch enters the hunk in the error file and continues as normal. (Note: reversed patch instructions cannot be detected with this method in the case of a normal diff and where the first command is an append, i.e. it should in fact have been a delete.)
Specifies the file to which the rejected patchs should be sent. -r not specified:
Pathname of the file to be patched. file not specified: |
Hint: Notes for patch senders
If you create patch files on a regular basis, it is recommended that the revision level be included as the first patch instruction. If you add the line "Prereq: " to the patch file, you can prevent users applying incorrect patches without being warned. Check whether the filenames have been specified correctly in the context diff header or in the "Index:" line. If you wish to make corrections in a subdirectory, inform the patch user that the -p option must be set. Avoid reversed patches where possible. Place patches that need to be together in the event of an error in separate files where possible. If patch files are created with rejected hunks, patch terminates with a non-zero exit status. If you want to apply a set of patches in a loop, you should check this exit status so that a subsequent patch will not be performed on a partly corrected file. If code was duplicated (e.g. using "#ifdef OLDCODE ... #else ... #endif"), patch cannot correct both versions. If the command can be executed at all, it may correct the wrong version and inform you that the corrections have been made successfully. If you use an already applied patch again, patch assumes that the patch in question is a reversed patch and offers to un-apply the patch. |
Locale
The following environment variables affect the execution of patch: LANG Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined. LC_ALL If set to a non-empty string value, override the values of all the other internationalization variables. LC_CTYPE Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other. LC_MESSAGES Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. LC_TIME Determine the locale for recognising the format of file timestamps written by the diff utility in a context-difference input file. NLSPATH Determine the location of message catalogs for the processing of LC_MESSAGES. |
Exit status
0 | Successful completion. |
1 | One or more rejected patch instructions were written to the error file. |
>1 | An error occurred. |
See also
diff, ed |