Format
patch [–bceflNnRsv]
[–B prefix] [–D symbol]
[–d dir] [–F n]
[–i patchfile]
[–o outfile] [–p n]
[–r rejectfile]
[file]
Description
patch reads
a patchfile that contains output from diff describing
changes from an old text file to a new text file. patch then
applies those changes to another text file.
Typically, you use patch if you are keeping
parallel versions of a file. When you make a set of changes to one
file, you can use patch to incorporate those
same changes in other versions of the file.
Options
- –B prefix
- Saves a copy of the original file in
a backup file. The backup file name is the name of the original file
preceded by the string prefix. If there
is already a file with this name, patch overwrites
it. When applying more than one patch to the same file, patch copies
only the original for the first patch. When you also specify –o outfile, patch does
not create prefixfile, but if outfile already
exists, it creates prefixoutfile.
- –b
- Saves a copy of the original file in
a backup file. The backup file name is the name of the original file
plus the suffix orig. If there is already a file
with that name, patch overwrites it. When
applying more than one patch to the same file, patch only
creates file orig. When
you also specify –o outfile, patch does
not create file-.orig,
but if outfile already exists, it creates outfile.orig.
- –c
- Interprets the patchfile as a context diff
file (the output of diff when –c or –C is
specified). You cannot use this option with –e or –n.
- –D symbol
- Marks changes with the C preprocessor construct:
#ifdef symbol
…
#endif
When you compile the resulting (patched) file,
you get the original file if symbol is not
defined, and the changed file if symbol is
defined.
- –d dir
- Changes the current directory to dir before
processing the patch.
- –e
- Interprets the patchfile as an ed script
(the output of diff when –e is
specified). You cannot use this option with –c or –n.
- –F n
- Specifies the number of lines of a context diff to ignore when
searching for a place to install a patch.
- –f
- Forces processing without requesting additional information from
the user.
- –i patchfile
- Reads the patchfile information from the file patchfile .
If you do not specify patchfile, patch reads
the information from the standard input.
- –l
- Matches any sequences of blanks in the patchfile to any sequence
of blanks in the input file. In other words, patch considers
two lines equivalent if the only difference between the two is their
spacing.
- –N
- Ignores any patches that have already been applied. By default, patch rejects
already-applied patches.
- –n
- Interprets the patchfile as normal diff output.
You cannot use this option with –c or –e.
- –o outfile
- Writes patched output to outfile instead
of to the original file. When you specify more than one patch to a
single file, patch applies the patches to
intermediate versions of the file created by previous patches, resulting
in multiple, concatenated versions of the file being written to outfile.
- –p n
- Deletes n components from the beginning
of all path names found in the patch file. If a path name is an absolute
path name (that is, starts with a slash), patch treats
the leading slash as the first component of the path, and patch –p 1
deletes the leading slash. Specifying –p 0
tells patch to use the full path names given
in the patchfile. If you do not specify
this option, patch only uses the basename
(the final path component).
- –R
- Reverses the sense of the patch script. In other words, patch behaves
as if the patch script shows the changes that make the new version
into the old version. You cannot use –R if
the patchfile is in ed script format.
With –R, patch attempts
to reverse each change recorded in the script before applying the
change. patch saves rejected differences
in reversed format (which means that you can check the rejections
to see if patch made the reversals correctly).
- –r rejectfile
- Records rejects in the file rejectfile,
instead of the default reject file name.
- –s
- Tells patch to remain silent until an
error occurs. Normally, patch writes information
about the results of the patching process to standard error (stderr).
- –v
- Displays the version number of patch and
then exits.
If you do not specify either the –b or –B option, patch attempts
to change the original file directly. If
you do not specify –c, –e,
or –n, patch looks
at the format of the diff output and tries
to determine which type of output the patch file contains.
If
you do not specify a file to be patched and the patchfile is
not in context format, patch prompts you
for the name of the file you want to patch.
If the patchfile is
in context format, patch tries to determine
the file name on its own. The first two lines of a context patch file
give the names of the old and new files that diff compared.
If only one of the files exists, patch patches
that file; if neither exists or both do, patch checks
for a line starting with a string Index: before asking
you for the name of the file to patch. If both files exist but
one of them is empty, the empty file will automatically be patched.
After patch has
determined the file to patch, it checks for a source control system
(SCCS) subdirectory in the current directory; if one exists, it tries
to obtain an editable version of that file by performing the source
code control system (SCCS) command get –e.
If patch cannot determine the file to patch,
it prompts you for the name of the file to use.
With a context
format patchfile, patch can
recognize when line numbers given in the patchfile do
not match line numbers in the file being patched. Thus, it can patch
a file with line counts that do not match the old file that was used
by diff. To do this, it takes these steps:
- For each section to be changed, patch starts
with the line number found in the patch file, plus or minus any adjustment
that must be made for the previous section.
- If the line at this location does not match the line in the patch
file, patch scans forward and backward for
a line that does match. If it finds a matching line, patch proceeds
to make the required changes. patch also
remembers the adjustment it had to make to find the matching line,
and uses this adjustment in the next section to be changed.
- If patch cannot find a line matching
the one in the patch file, it tries to find the line using the lines
given as context. It ignores the first and last two lines of the context
and goes searching again. If it finds a match this time, it makes
the change and remembers the adjustment.
- If a search ignoring the first and last lines of the context fails, patch searches
one more time, ignoring the first two and last two lines of the context.
If it finds a match, it makes the changes and remembers the adjustment.
- If patch still cannot find a match,
it writes the unmatching portion to the reject file.
It then tries to process the next section of changes. Thus, the reject
file contains the sections that patch is
not able to change. Line numbers on sections in the reject file may
be different than those in the patchfile, because patch adjusts
them using the adjustment that patch calculated
for preceding sections.
To some extent, patch tries the
same process if the patch file is in normal format rather than context
format. Because the patch file does not contain the context information, patch has
less to work with and probably creates more rejects. patch always
writes the rejectfile in context format,
regardless of the format of the patchfile.
By
default, the reject file has the same name as the original file,
plus the suffix .rej. You can use –r to
specify a different reject file on the command line. If the reject
file already exists, patch overwrites it.
If
you do not specify –R, patch starts
out with the assumption that the patch file could be normal or reversed.
Therefore if the first change is rejected, patch tries
the reverse change to see if that one works. If the reverse change
is also rejected, patch continues with other
changes in the file, trying both forward changes and reverses until
one of them works. If the one that works is a forward change, patch attempts
only forward changes for the rest of the file. If the one that works
is a reverse change, patch issues a message
to this effect and ask if it should treat all the changes as reverse
ones. However, if the –R option is specified
on the command line, it is assumed to hold for all changes in the
patch file.
The patch file can contain output from several diff comparisons. patch treats
each collection of changes as a separate patch file, and with each, patch may
prompt you for the name of the file you want to patch.
Localization
patch uses
the following localization variables:
- LANG
- LC_ALL
- LC_CTYPE
- LC_MESSAGES
- LC_TIME
- LC_SYNTAX
- NLSPATH
See Localization for more
information.
Exit values
- 0
- Successful completion
- 1
- There were one or more rejects
- >1
- An error occurred
Portability
POSIX.2 User Portability Extension, X/Open Portability Guide, UNIX systems.
The –B, –F, –f, –s,
and –v options are not extensions to the
POSIX standard.
Related information
diff, ed