PATCH(1POSIX)
π PROLOG π
okay bestie, real talk before we even start: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so like, go consult the corresponding Linux manual page for the deets on Linux behavior, fr), or honestly the interface may not be implemented on Linux at all. π
β¨ NAME β¨
patch β apply changes to files π©Ή
πΈ SYNOPSIS πΈ
patch [-blNR] [-c|-e|-n|-u] [-d dir] [-D define] [-i patchfile]
[-o outfile] [-p num] [-r rejectfile] [file]
π DESCRIPTION π
so. The patch utility shall read a source (patch) file that contains any of four forms of difference (diff) listings β the kind produced by the diff utility β and apply those differences to a file. Those four flavors? normal, copied context, unified context, or in the style of ed. By default, patch shall read from the standard input. π
The patch utility shall attempt to determine the type of the diff listing all by itself, unless you overrule it with a -c, -e, -n, or -u option. it's giving main character energy but also it's giving "I can figure it out myself". π¦
If the patch file contains more than one patch, patch shall attempt to apply each of them as if they came from separate patch files. (In this case, the application shall ensure that the name of the patch file is determinable for each diff listing.) like, it can't read your mind, so make sure the names are findable, okay? π
β¨ OPTIONS β¨
The patch utility shall conform to the Base Definitions volume of POSIX.1β2017, Section 12.2, Utility Syntax Guidelines. yes bestie, it plays by the rules.
The following options shall be supported:
-bβ Save a copy of the original contents of each modified file, before the differences are applied, in a file of the same name with the suffix.origappended to it. If the file already exists, it shall be overwritten; if multiple patches are applied to the same file, the.origfile shall be written only for the first patch. When the-o outfileoption is also specified,file.origshall not be created but, ifoutfilealready exists,outfile.origshall be created. it's a little backup bestie, treasure it. π₯Ί-cβ Interpret the patch file as a copied context difference (that's the output of the utilitydiffwhen the-cor-Coptions are specified).-d dirβ Change the current directory to dir before processing, as described in the EXTENDED DESCRIPTION section. packing up and moving house, basically. π¦-D defineβ Mark changes with one of the following C preprocessor constructs:
#ifdef define
...
#endif
#ifndef define
...
#endif
optionally combined with the C preprocessor construct #else. If the patched file is processed with the C preprocessor, where the macro define is defined, the output shall contain the changes from the patch file; otherwise, the output shall not contain the patches specified in the patch file. choose your own adventure, but make it preprocessor. π
-eβ Interpret the patch file as anedscript, rather than a diff script. retro chic. π-i patchfileβ Read the patch information from the file named by the pathname patchfile, rather than the standard input. no more typing into the void. π-lβ (The letter ell.) Cause any sequence of<blank>characters in the difference script to match any sequence of<blank>characters in the input file. Other characters shall be matched exactly. blanks are flexible, everything else is on a leash. β-nβ Interpret the script as a normal difference.-Nβ Ignore patches where the differences have already been applied to the file; by default, already-applied patches shall be rejected. so-Nis the chill forgiving one, default is the strict one. π-o outfileβ Instead of modifying the files (specified by the file operand or the difference listings) directly, write a copy of the file referenced by each patch, with the appropriate differences applied, to outfile. Multiple patches for a single file shall be applied to the intermediate versions of the file created by any previous patches, and shall result in multiple, concatenated versions of the file being written to outfile. stacking them like a lil tower. π₯-p numβ For all pathnames in the patch file that indicate the names of files to be patched, delete num pathname components from the beginning of each pathname. If the pathname in the patch file is absolute, any leading<slash>characters shall be considered the first component (that is,-p 1shall remove the leading<slash>characters). Specifying-p 0shall cause the full pathname to be used. If-pis not specified, only the basename (the final pathname component) shall be used. it's giving βοΈ to your paths, and honestly, iconic.-Rβ Reverse the sense of the patch script; that is, assume that the difference script was created from the new version to the old version. The-Roption cannot be used withedscripts. Thepatchutility shall attempt to reverse each portion of the script before applying it. Rejected differences shall be saved in swapped format. If this option is not specified, and until a portion of the patch file is successfully applied,patchattempts to apply each portion in its reversed sense as well as in its normal sense. If the attempt is successful, the user shall be prompted to determine whether the-Roption should be set. so it'll lowkey ask you if you meant to flip it. polite! π«Ά-r rejectfileβ Override the default reject filename. In the default case, the reject file shall have the same name as the output file, with the suffix.rejappended to it; see Patch Application. your rejects deserve their own lil home. π-uβ Interpret the patch file as a unified context difference (the output of thediffutility when the-uor-Uoptions are specified). the people's favorite, ngl. πΈ
π¦ OPERANDS π¦
The following operand shall be supported:
- file β A pathname of a file to patch.
π STDIN π
See the INPUT FILES section. yeah, go look, it's right there. π
π INPUT FILES π
Input files shall be text files. that's it, that's the rule. no binary chaos here please. π§
π ENVIRONMENT VARIABLES π
The following environment variables shall affect the execution of patch:
LANGβ Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of POSIX.1β2017, Section 8.2, Internationalization Variables for the precedence of internationalization variables used to determine the values of locale categories.) the fallback bestie. π«ΆLC_ALLβ If set to a non-empty string value, override the values of all the other internationalization variables. it said ALL and it meant it. πLC_COLLATEβ Determine the locale for the behavior of ranges, equivalence classes, and multi-character collating elements used in the extended regular expression defined for theyesexprlocale keyword in theLC_MESSAGEScategory.LC_CTYPEβ Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multi-byte characters in arguments and input files), and the behavior of character classes used in the extended regular expression defined for theyesexprlocale keyword in theLC_MESSAGEScategory. it's deciding what even counts as a character, which is a whole vibe. β¨LC_MESSAGESβ Determine the locale used to process affirmative responses, and the locale used to affect the format and contents of diagnostic messages and prompts written to standard error. this is the one that decides what "yes" means when it asks you stuff. πNLSPATHβ Determine the location of message catalogs for the processing ofLC_MESSAGES.LC_TIMEβ Determine the locale for recognizing the format of file timestamps written by thediffutility in a context-difference input file. timestamps, but make it localized. β
π ASYNCHRONOUS EVENTS π
Default. no special treatment, it just does the normal thing, periodt.
β¨ STDOUT β¨
Not used. it's not a yapper. it puts its work in the files, not your terminal. π
π STDERR π
The standard error shall be used for diagnostic and informational messages. so when it talks to you, it's over here. listen up. π
πΈ OUTPUT FILES πΈ
The output of the patch utility, the save files (.orig suffixes), and the reject files (.rej suffixes) shall be text files. all of them. every single one. π©Ή
π EXTENDED DESCRIPTION π
okay so listen β a patch file can contain patching instructions for more than one file, bestie. filenames get figured out the way it says in Filename Determination. and when you pass the -b option, for every single patched file, the original gets saved in a file with the same name plus .orig stuck on the end. β¨
for each patched file, a reject file might also pop into existence, as noted over in Patch Application. and if you didn't pass -r? then that reject file's name is just the original filename with .rej appended. simple. iconic. π
πΈ Patch File Format πΈ
the patch file shall contain zero or more lines of header info, followed by one or more patches. each patch shall contain zero or more lines of filename identification in the format produced by the -c, -C, -u, or -U options of the diff utility, plus one or more sets of diff output β which everybody customarily calls hunks. π¦
the patch utility shall recognize this expression in the header information:
Index: pathnameβ the file to be patched is named pathname. that's it, that's the whole vibe.
if all the lines (headers included) within a patch begin with the same leading sequence of <blank> characters, patch shall strip that sequence off before proceeding. π§Ή
inside each patch, if the type of difference is common context, patch shall recognize these expressions:
*** filename timestampβ the patches arose from filename.--- filename timestampβ the patches should be applied to filename.
and if the type of difference is unified context, patch shall recognize these:
--- filename timestampβ the patches arose from filename.+++ filename timestampβ the patches should be applied to filename.
each hunk within a patch shall be the diff output needed to change a line range within the original file. and the line numbers for successive hunks within a patch shall occur in ascending order. no chaotic out-of-order nonsense, we're not animals. π
π¦ Filename Determination π¦
if no file operand is specified, patch shall do these steps to figure out which filename to use:
- if the type of diff is context, the
patchutility shall delete pathname components (as specified by the-poption) from the filename on the line beginning with***(if copied context) or---(if unified context), then test whether this file exists relative to the current directory (or the directory given with the-doption). if the file exists,patchshall use this filename. β - if the type of diff is context,
patchshall delete the pathname components (as specified by the-poption) from the filename on the line beginning with---(if copied context) or+++(if unified context), then test whether this file exists relative to the current directory (or the directory given with the-doption). if it exists,patchshall use this filename. β - if the header information contains a line beginning with the string
Index:,patchshall delete pathname components (as specified by the-poption) from this line, then test whether this file exists relative to the current directory (or the directory specified with the-doption). if it exists,patchshall use this filename. β - if an SCCS directory exists in the current directory,
patchshall attempt to perform aget -e SCCS/s.filenamecommand to retrieve an editable version of the file. if the file exists,patchshall use this filename. β - the
patchutility shall write a prompt to standard output and request a filename interactively from the controlling terminal (for example,/dev/tty). π
π Patch Application π
if the -c, -e, -n, or -u option is present, patch shall interpret the information inside each hunk as a copied context difference, an ed difference, a normal difference, or a unified context difference, respectively. and if none of those options are there? patch shall figure out the type of difference based on the format of the info inside the hunk. detective work, honestly. π
for each hunk, patch shall begin searching for the place to apply the patch at the line number at the beginning of the hunk, plus or minus any offset used in applying the previous hunk. if lines matching the hunk context aren't found, patch shall scan both forwards and backwards at least 1000 bytes looking for a set of lines that match the hunk context. π₯
if no such place is found and it's a context difference, then another scan shall take place, ignoring the first and last line of context. if that fails, the first two and last two lines of context shall be ignored and yet another scan shall be made. implementations may search more extensively for installation locations β go off, honestly. π
if no location can be found at all, patch shall append the hunk to the reject file. a rejected hunk that's a copied context difference, an ed difference, or a normal difference shall be written in copied-context-difference format, regardless of the format of the patch file. and it is implementation-defined whether a rejected hunk that's a unified context difference gets written in copied-context-difference format or in unified-context-difference format. π
if the input was a normal or ed-style difference, the reject file may contain differences with zero lines of context. and heads up bestie β the line numbers on the hunks in the reject file may be different from the line numbers in the patch file, because they shall reflect the approximate locations for the failed hunks in the new file rather than the old one. π
if the type of patch is an ed diff, the implementation may accomplish the patching by invoking the ed utility. lowkey outsourcing. π
β¨ EXIT STATUS β¨
these are the exit values that shall be returned:
0β successful completion. slay. π1β one or more lines were written to a reject file. π¬>1β an error occurred. π
π CONSEQUENCES OF ERRORS π
patches that cannot be correctly placed in the file shall be written to a reject file. that's the whole consequence, fr fr.
the following sections are informative. (read: the standard is being chatty, not commanding.)
πΈ APPLICATION USAGE πΈ
the -R option does not work with ed scripts, because there's simply too little information to reconstruct the reverse operation. it's giving "not enough data to work with." π
the -p option makes it possible to customize a patch file to local user directory structures without manually editing the patch file. for example, if the filename in the patch file was:
/curds/whey/src/blurfl/blurfl.c
then setting -p 0 gives the entire pathname unmodified. -p 1 gives:
curds/whey/src/blurfl/blurfl.c
without the leading <slash>. -p 4 gives:
blurfl/blurfl.c
and not specifying -p at all gives:
blurfl.c .
and yes that trailing space in the original is a whole thing. we keep it. π«Ά
π EXAMPLES π
none. absolutely none. the well is dry, bestie. π
π RATIONALE π
some of the functionality in historical patch implementations was not specified. the following documents those features present in historical implementations that have not been specified.
a deleted piece of functionality was the '+' pseudo-option, which allowed an additional set of options and a patch file operand to be given. this was seen as being insufficiently useful to standardize.
in historical implementations, if the string "Prereq:" appeared in the header, the patch utility would search for the corresponding version information (the string specified in the header, delimited by <blank> characters or the beginning or end of a line or the file) anywhere in the original file. this was deleted as too simplistic and an insufficiently trustworthy mechanism to standardize. for example, if:
Prereq: 1.2
were in the header, the presence of a delimited 1.2 anywhere in the file would satisfy the prerequisite. imagine. π
these options were dropped from historical implementations of patch as insufficiently useful to standardize:
-bβ the-boption historically provided a method for changing the name extension of the backup file from the default.orig. this option has been modified and retained in this volume of POSIX.1-2017. glow-up. β¨-Fβ the-Foption specified the number of lines of a context diff to ignore when searching for a place to install a patch.-fβ the-foption historically causedpatchnot to request additional information from the user.-rβ the-roption historically provided a method of overriding the extension of the reject file from the default.rej.-sβ the-soption historically causedpatchto work silently unless an error occurred.-xβ the-xoption historically set internal debugging flags.
in some file system implementations, saving a .orig file may produce unwanted results. in the case of 12, 13, or 14-character filenames (on file systems supporting 14-character maximum filenames), the .orig file overwrites the new file. π the reject file may also exceed this filename limit. it was suggested, due to some historical practice, that a <tilde> ('~') suffix be used instead of .orig, and some other character instead of the .rej suffix. this was rejected because it is not obvious to the user which file is which. the suffixes .orig and .rej are clearer and more understandable. clarity ate. π
the -b option has the opposite sense in some historical implementations β do not save the .orig file. the default case here is not to save the files, making patch behave more consistently with the other standard utilities. consistency is the main character. π
the -w option in early proposals was changed to -l to match historical practice.
the -N option was included because without it, a non-interactive application cannot reject previously applied patches. for example, if a user is piping the output of diff into the patch utility, and the user only wants to patch a file to a newer version non-interactively, the -N option is required.
changes to the -l option description were proposed to allow matching across <newline> characters in addition to just <blank> characters. since this is not historical practice, and since some ambiguities could result, it is suggested that future developments in this area utilize another option letter, such as -L.
the -u option of GNU patch has been added, along with support for unified context formats. π¦
β FUTURE DIRECTIONS β
none. again. we're at peace with it. π§
π«Ά SEE ALSO π«Ά
diffed- the Base Definitions volume of POSIX.1-2017, Chapter 8, Environment Variables, Section 12.2, Utility Syntax Guidelines
π COPYRIGHT π
Okay bestie, here comes the legal bit β I know, I know, but we don't skip it, we never skip it, that's the whole vibe. β¨
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group.
π«Ά Real talk though: if this version and the original IEEE and The Open Group Standard ever disagree about something, the original is the referee. Like, the original wins, no debate, that's the rule. You can get the original Standard online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
And that's a wrap, bestie!! π You read the whole thing, every last flag and every last footnote, and honestly? iconic behaviour. This page was served up by IEEE/The Open Group, dated 2017, and it's filed under PATCH(1POSIX) β so go patch something responsibly, and remember: no cap, read the docs first. ππ¦πΈ
mwah π
π¦ where this page comes from
- debian bookworm: manpages-posix 2017a-2 Β· Other
- debian trixie: manpages-posix 2017a-2 Β· Other
- ubuntu jammy: manpages-posix 2017a-2 Β· Other
- ubuntu noble: manpages-posix 2017a-2 Β· Other
- ubuntu resolute: manpages-posix 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms