打补丁patch 命令使用

时间:2022-01-19 09:23:33

patch 命令用于打补丁,补丁文件是使用diff产生的

patch 命令语法

patch [  -b [ -B Prefix ] ] [  -f ] [ -l ] [ -N ] [ -R ] [  -s ] [  -v ] [  -c | -e | -n ] [  -d Directory ] [  -D Define ] [  -F Number ] [  -i PatchFile ] [  -o OutFile ] [  -p Number ] [  -r RejectFile ] [  -x Number ] [ File ]


patch 命令失败或拒绝接受补丁时,会产生一个和原文件同名,以".rej"为后缀的差异文件。
   当知道 -b 时,会产生一个和原文件同名,以".orig"为后缀的备份文件。


常使用的 patch 参数:


 -p 指定目录级别(从路径全称中除去几层目录)


 如,如果补丁文件包含路径名称 /curds/whey/src/blurfl/blurfl.c,那么:


  -p 0 使用完整路径名
  -p 1 除去前导斜杠,留下 curds/whey/src/blurfl/blurfl.c。
  -p 4 除去前导斜杠和前三个目录,留下 blurfl/blurfl.c。



    -d Directory 打补丁前,更改当前目录到指定目录


 -i PatchFile 从指定文件,而不是从标准输入中读取补丁信息


 -R 逆向补丁,这个选项在防止打错补丁很有用处



补丁的产生一般用


diff -Nrua   a  b > c.patch


如:


#diff -Nrua linux-2.6.14/Makefile  linux-2.6.26/Makefile >c.patch
#cat c.patch


--- linux-2.6.14/Makefile 2008-07-30 16:54:20.000000000 +0800
+++ linux-2.6.26/Makefile 2008-07-14 05:51:29.000000000 +0800
@@ -1,8 +1,8 @@
 VERSION = 2
 PATCHLEVEL = 6
-SUBLEVEL = 14
+SUBLEVEL = 26
 EXTRAVERSION =
-NAME=Affluent Albatross
+NAME = Rotary Wombat
 
 # *DOCUMENTATION*
 # To see a list of typical targets execute "make help"


 


--- 的文件表示将被打补丁的文件 如:linux-2.6.14/Makefile
+++ 的文件表示补丁来源文件   如:linux-2.6.26/Makefile



应用 patch
 #ls
 c.patch  linux-2.6.14  linux-2.6.26
 #cd linux-2.6.14
 #patch -p1 <../c.patch


如果有多个补丁要打,则应该注意打补丁的顺序!


Patch

Patch多指 补丁的意思比如内存补丁、文件补丁等, 也是电脑命令程序的一种。

1 命令应用

用途

对文件应用更改。

语法

patch [ -b [ -B Prefix ] ] [ -f ] [ -l ] [ -N ] [ -R ] [ -s ] [ -v ] [ -c | -e | -n ] [ -d Directory ] [ -D Define ] [ -F Number ] [ -i PatchFile ] [ -o OutFile ] [ -p Number ] [ -r RejectFile ] [ -x Number ] [ File ]

描述

patch 命令读取如何更改文件的源文件指示信息,然后应用这些更改。源文件包含由 diff 命令产生的差别列表(或者 diff 列表)。差异列表是比较两个文件和构建关于如何纠正差别的指示信息的结果。缺省情况下,patch 命令使用从标准输入读入的源文件,但是使用 -i 标志和 PatchFile 变量可以覆盖此设置。
差异列表有三种格式:正常、上下文或者是 ed 编辑器风格。patch 命令确定差异列表格式,除非被 -c、-e 或 -n 标志否决。
缺省情况下,文件的打过 补丁的版本替换原始版本。指定 -b 标志时,每个 补丁文件的原文件保存在同名的文件中,只是在文件名后附加了后缀 .orig。使用 -o 标志也可以指定输出的目的地。
-p 标志使得无需手工编辑 补丁文件就可以定制补丁文件到 本地用户目录结构中。通过指定从路径全称除去多少部分可以做到这一点。例如,如果 补丁文件包含路径名称 /curds/whey/src/blurfl/blurfl.c,那么:
* -p 0 导致使用完整 路径名。
* -p 1 除去前导斜杠,留下 curds/whey/src/blurfl/blurfl.c。
* -p 4 除去前导 斜杠和前三个目录,留下 blurfl/blurfl.c。
不指定 -p 标志使得 patch 命令使用基本名称。在上面的示例中,此文件为 blurfl.c。
补丁 文件格式
补丁文件必须包含单行或多行头信息,后跟单个或多个补丁。每个 补丁必须包含一行或多行文件名标识,其格式由 diff -c 命令和单个或多个 diff 命令输出集产生,通常称为 hunks。
patch 命令跳过 补丁文件中的任何前导文本,然后应用实际的差异列表,并且跳过任何后续文本。因而,可以将包含差异列表的文件或消息当成 补丁文件使用,此时 patch 命令仍然有效。在这种情况下,如果整个差异列表使用一致的数量 缩进,patch 命令也会调整其间距。
要更改原始文件中的行范围,每一 补丁中的块(hunk)必须为单独的差异列表。 补丁内连续块(hunk)的行号必须以升序方式出现。

2 文件名

如果没有指定 File 参数,要获得供编辑的文件名,patch 命令会执行下面的步骤:
1. 在上下文差异列表的头部分,文件名由以 ***(三个星号)或者 ---(三个破折号)开头的行确定。以 *** 开头的行表示获取 补丁的文件,然而以 --- 开头的行表示应该应用补丁的文件名。选择存在文件的最短名称。
2. 如果在前导文本中有 Index: 行,patch 命令尝试使用来自于那一行中的文件名。
3. 上下文差异 文件头优先于 Index: 行。
4. 如果从前导文本中不能确定文件名,patch 命令提示输入需要打 补丁的文件名。
5. 如果不能找到原始文件,但是有适合的 SCCS 或 RCS 文件可用,patch 命令尝试获取或检出文件。
6. 如果前导文本包含 Prereq: 行,patch 命令从先决条件行中获取第一个词(通常是版本号)并且检查输入文件看是否能找到那个词。如果找不到,patch 命令在继续运行前会提示确认。

3 应用程序

如果 补丁文件包含不止一个补丁,patch 命令尝试应用每个差异列表,就好像它来自于单独的补丁文件。在这种情况下,为每个差异列表确定需要打 补丁的文件名,并且审查每个差异列表前的头文本以获得如文件名和修订版级别的信息。
如果指定 -c、 -e 或 -n 标志,patch 命令将每块(hunk)内的信息分别解释成上下文差别、ed 编辑器差别或者正常差别。否则,patch 命令确定基于块(hunk)内信息格式的差别类型。
patch 命令通过获取块(hunk)的首行序号和添加或减去由于应用前一块(hunk)而产生的任何行偏移来搜索位置以应用每一块(hunk)。如果在这一行位置不可能有精确匹配,patch 命令前后搜索以获取与块(hunk)的内容精确匹配的行集合。
如果找不到这些位置,且如果 patch 命令正在应用上下文差异列表,patch 命令能进行非精确搜索。fuzz factor 指定了非精确匹配的行数目。如果模糊因子设置成 1 或者更大,patch 命令执行第二次扫描,这一次忽略上下文的第一行和最后一行。如果没有匹配结果,且最大模糊因子设置成 2 或者更大, patch 命令执行第三次扫描,这一次会忽略上下文的前两行和最后两行。(缺省模糊因子最大值为 2)。如果找不到匹配的位置,patch 命令在拒绝文件中放置块(hunk)。创建拒绝文件时,其名称和输出文件一样,只是在文件名有后缀 .rej。使用 -r 标志可以覆盖此 命名约定
以上下文差异列表格式写拒绝块(hunk),而不管 补丁文件的格式如何。如果输入是正常差别或 ed 编辑器样式差别,拒绝文件可能包含上下文格式零行差别。拒绝文件中块(hunk)的行编号可能与 补丁文件中的行编号不同。这是因为拒绝文件的行编号反映了新文件而不是老文件中故障块(hunk)的大约位置。
当完成每块(hunk)后,patch 命令会告诉您该块(hunk)是成功还是失败。也可以获知为每块(hunk)假定的新行编号。如果这与差异列表中指定的行编号不同,就会报告偏移量。patch 命令也说明是否使用模糊因子来进行匹配。
注:单一的大偏移可能表示块(hunk)安装位置不正确。模糊因子的使用可能表示布局不正确。

4  准备补丁

原则

准备将 补丁装载给其它用户的程序员应该考虑下面的附加原则:
* 如果想两次应用同一 补丁,patch 命令假定第二个 应用程序应该是逆向补丁,并且提示确认此逆向。因此,避免发送出这些逆向 补丁,因为它使用户疑惑他们是否已经应用了此补丁。
* 建议保留使用最新 补丁级别更新的 patchlevel.h 文件。 补丁级别可以用作所发送的补丁文件中的第一个差异列表。如果 补丁中包含 Prereq: 行,用户不能应用顺序混乱的补丁,同时不收到警告。
* 在上下文差异列表的头中或者使用 Index: 行以确保正确指定了文件名。如果正在子目录中打某些 补丁,请确保通知补丁用户在需要时指定 -p 标志。
* 可以通过发送差异列表创建文件,此列表比较一个空文件和想要创建的文件。然而,这个方法只有在想要创建的文件的确不存在于目标目录时才有效。
* 虽然可以将许多差分列表放置到一个文件中,但是将相关 补丁分组到单独的文件中会更好。
* patch 命令不能说明 ed 脚本中的行编号是否正确,只能在当它找到更改或删除命令时才能检测正常差异列表中不正确的行编号。使用模糊因子为 3 的上下文差异列表可能有同样的行编号问题。除非添加了一个适当的交互式接口,在这种情形下才使用上下文差异列表来检测更改的正确性。编译无误通常表示 补丁工作正常,但是它并不表示没有错误。
* 只有当 补丁应用到与生成补丁的完全同一版本的文件中,才保证 patch 命令的结果。
* 如果代码重复,例如:
#ifdef
... NEWCODE
#else
... OLDCODE
# endif
patch 命令不能为两个版本都打 补丁。如果 patch 命令成功,它可能 补丁了错误版本但是返回了一个成功的退出状态。

标志

-b 在应用差别前保存每个修改后文件的副本。复制的原始文件归档时与原文件同名且添加了后缀 .orig。如果使用那个名称的文件已经存在,它就被覆盖。如果对同一文件应用多个 补丁,原始文件只生成一个副本(在第一次补丁时)。如果也指定 -o OutFile 标志,就不会创建 .orig 文件。但是如果指定文件已经存在,就创建 OutFile.orig。
-B Prefix 指定备份文件名称的前缀。此标志只有在和 -b 标志连接使用时才有效。
-c 将 补丁文件解释成上下文差异列表(diff -c 或 diff -C 命令的输出)。此标志不能和 -e 或 -n 标志一起使用。
-d Directory 在处理前,更改 当前目录到指定目录。
-D Define 使用下面的 C 预处理器构造标记更改:
#ifdef Define
... (NEWCODE)
#else
... (OLDCODE)
#endif /* Define */
Define 变量用作差分符号。此标志只有当正常或上下文格式差异列表用作 补丁文件时才有效。
-e 将 补丁文件解释成 ed 编辑器 脚本。此标志不能和 -c 或 -n 标志一起使用。
-f 禁止查询用户。要禁止注释,使用 -s 标志。
-F Number 设置最大模糊因子。此标志只应用于上下文差异列表,它使 patch 命令在确定块(hunk)的安装位置时忽略指定行编号。如果没有指定 -F 标志,缺省模糊因子为 2。此因子不可以设置成大于上下文差异列表中内容的行的数目(通常为 3)。
注:较大的模糊因子会增加错误 补丁的可能性。
-i PatchFile 从指定文件,而不是从标准输入中读取 补丁信息。
-l (L 的小写)使差异列表 脚本中的任何空格字符序列匹配输入文件中的任何空格字符序列。精确匹配其它字符。
-n 将 脚本解释成正常差异列表。此标志不能和 -c 或 -e 标志一起使用。
-N 忽略差别已经向文件应用了的 补丁。缺省情况下,会拒绝已经应用的 补丁
-o OutFile 复制要打 补丁的文件,然后应用更改,接着将修改版本写到指定的输出文件。单个文件的多个 补丁应用于以前补丁所创建的文件的中间版本。因此,多 补丁会生成输出文件的多个连接版本。
-p Number 设置路径名的剥离数目,它控制如何处理在 补丁文件中找到的路径名称。如果将文件保留在与指定路径不同的目录中,此标志就有用。剥离数目指定了从路径名前去除多少个 斜杠。也去除所有中间的目录名。例如,假定 补丁文件指定 /u/leon/src/blurf1/blurf1.c:
* -p 0 留下未修改的完整路径名。
* -p 1 除去前导斜杠,留下 u/leon/src/blurf1/blurf1.c。
* -p 4 卸下四个斜杠和三个目录,留下 blurf1/blurf1.c。
如果没有指定 -p 标志,只使用基本名称(最后的路径名称组件)。此标志只有在没有指定 File 参数时才起作用。
-r RejectFile 覆盖缺省拒绝文件名。通过附加后缀 .rej 到原始文件名中,就形成了缺省拒绝文件名。
-R 逆向 补丁 脚本理解。例如,如果从新版本到旧版本创建差异列表,使用 -R 标志使 patch 命令在应用前逆向 脚本的每个部分。以交换格式保存拒绝差别。 -R 标志不能和 ed 脚本一起使用,因为其中只有很少的信息可以重新构造逆向操作。如果没有指定 -R 标志,patch 命令尝试以逆向理解和正常理解应用每个部分,直到成功应用 补丁文件的每一部分。如果尝试成功,提示用户确定是否应该设置 -R 标志。
注:如果此方法和第一个命令是附加命令(就是说,逆序是删除)的正常差异列表一起使用,它就不能检测逆向 补丁。因为空上下文无论在何处都匹配,所以附加总是成功的。幸运的是,大多数 补丁是添加或更改行而不是删除行。因此大多数逆序的正常差异列表以一个删除开始,它导致故障,并且引起启发(heuristic)。
-s 静默地进行 补丁,直到发生错误。
-v 打印修订版头和 补丁级别。如果 -v 标志和其它标志一起使用,就忽略其它标志。
-x Number 设置内部调试标志。此标志只适用于 patch 命令开发者。
退出状态
返回下面的出口值:
0 成功完成。
1 产生错误。

示例

1. 要将 difflisting 文件中的差异列表应用到 prog.c 文件,请输入:
patch -i difflisting prog.c
2. 要保存 prog.c 文件的原始版本,请输入:
patch -b -i difflisting prog.c
它将更改应用到 prog.c 并且在 prog.c.orig 文件中保存 prog.c 的原始内容。
3. 要打 补丁到 prog.c 文件而不改变原始版本,请输入:
patch -i difflisting -o prog.new prog.c
它将 prog.c 当成 源文件使用,但是更改后的版本写到名为 prog.new 的文件中。

文件

/usr/bin/patch 包含 patch 命令。


AIX Version 4.3 Commands Reference, Volume 4


patch Command

Purpose

Applies changes to files.

Syntax

patch-b [-B Prefix ] ] [ -f ] [-l ] [ -N ] [ -R ] [ -s ] [ -v ] [ -c |-e | -n ] [ -dDirectory ] [ -D Define ] [ -FNumber ] [ -i PatchFile ] [ -o OutFile ] [ -p Number ] [ -rRejectFile ] [ -x Number ] [ File ]

Description

The patch command reads a source file's instructions on how to change a file, then applies the changes. The source file contains difference listings (ordiff listings) produced by the diff command. The diff listings are the result of comparing two files and building instructions about how to reconcile the differences. By default, thepatch command uses the source file read from standard input, but this can be overridden using the-i flag and the PatchFile variable.

Diff listings come in three formats: normal, context, or in the style of the ed editor. Thepatch command determines the diff listing format, unless overruled by the-c, -e, or -n flags.

By default, the patched version of a file replaces the original version. When the-b flag is specified, the original of each patched file is saved in a file of the same name with the suffix.orig appended to it. You may also specify where you want the output to go with the-o flag.

The -p flag makes it possible to customize a patch file to local user directory structures without manually editing the patch file. This is done by specifying how many components to remove from the full path name. For example, if the patch file contained the path name /curds/whey/src/blurfl/blurfl.c, then:

  • -p 0 causes the entire path name to be used.
  • -p 1 removes the leading slash, leaving curds/whey/src/blurfl/blurfl.c.
  • -p 4 removes leading slashes and the first three directories, leavingblurfl/blurfl.c.

Not specifying the -p flag causes the patch command to use the base name. In the above example, that would beblurfl.c.

Patch File Format

The patch file must contain one or more lines of header information followed by one or more patches. Each patch must contain one or more lines of file name identification in the format produced by thediff -c command, and one or more sets of diff command output, customarily called hunks.

The patch command skips any leading text in a patch file, applies the actual diff listing, and skips any trailing text. Thus, you could use as a patch file an article or message that includes a diff listing, and thepatch command would still work. In such a case, if the entire diff listing is indented by a consistent amount, thepatch command will also adjust for that spacing.

To change a line range within the original file, each hunk within a patch must be a separate diff listing. The line numbers for successive hunks within a patch must occur in ascending order.

File Name Determination

If no File parameter is specified, the patch command performs the following steps to obtain the name of the file to edit:

  1. In the header of a context diff listing, the file name is determined from lines beginning with *** (three asterisks) or --- (three dashes). A line beginning with *** indicates the name of the file from which the patches were taken, while a line beginning with --- indicates the name of the file to which the patches should be applied. The shortest name of an existing file is selected.
  2. If there is an Index: line in the leading text, the patch command tries to use the file name from that line.
  3. A context diff header takes precedence over an Index: line.
  4. If no file name can be determined from the leading text, the patch command prompts you for the name of the file to patch.
  5. If the original file cannot be found, but a suitable SCCS or RCS file is available, thepatch command attempts to get or check out the file.
  6. If the leading text contains a Prereq: line, the patch command takes the first word from the prerequisites line (normally a version number) and checks the input file to see if that word can be found. If not, thepatch command prompts you for confirmation before proceeding.

Patch Application

If the patch file contains more than one patch, the patch command tries to apply each diff listing as if it came from a separate patch file. In this case, the name of the file to patch is determined for each diff listing, and the header text before each diff listing is examined for information such as file name and revision level.

If you specify the -c, -e, or -n flag, thepatch command interprets information within each hunk as a context difference, an ed editor difference, or a normal difference, respectively. Otherwise, thepatch command determines the type of difference based on the format of the information within the hunk.

The patch command searches for the place to apply each hunk by taking the first line number of the hunk and adding or subtracting any line offset caused by applying the previous hunk. If an exact match is not possible at this line location, the patch command scans both forward and backward for a set of lines matching the hunk's content exactly.

If no such place is found, and if the patch command is applying a context diff listing, thepatch command can search for a less exact match. A fuzz factor specifies how many lines can be inexactly matched. If the fuzz factor is set to 1 or more, thepatch command performs a second scan, this time ignoring the first and last line of context. If no match results, and the maximum fuzz factor is set to 2 or more, thepatch command performs a third scan, this time ignoring the first two lines and the last two lines of the context. (The default maximum fuzz factor is 2.) If no match is found, thepatch command places the hunk in a reject file. The reject file is created with the same name as the output file and the suffix.rej. This naming convention can be overridden by using the -r flag.

The rejected hunk is written in context diff listing form, regardless of the format of the patch file. If the input was a normal or ed editor style difference, the reject file may contain differences with zero lines of context format. The line numbers on the hunks in the reject file may be different from the line numbers in the patch file. This is because the reject file line numbers reflect the approximate locations for the failed hunks in the new file rather than the old one.

As each hunk is completed, the patch command tells you whether the hunk succeeded or failed. You are also informed of the new line number assumed for each hunk. If this is different from the line number specified in the diff listing, you are notified of the offset. The patch command also tells you if a fuzz factor was used to make the match.

Note: A single large offset may be an indication that a hunk was installed in the wrong place. Use of a fuzz factor may also indicate bad placement.

Preparing Patches for Other Users

Programmers preparing patches that will be shipped to other users should consider the following additional guidelines:

  • If you try to apply the same patch twice, the patch command assumes the second application should be a reverse patch and prompts you for confirmation of this reversal. Therefore, avoid sending out reversed patches, since this makes users wonder whether they already applied the patch.
  • It is recommended that you keep a patchlevel.h file that is updated with the latest patch level. The patch level can then be used as the first diff listing in the patch file you send out. If your patch includes aPrereq: line, users cannot apply patches out of order without receiving a warning.
  • Make sure you specify the file names correctly, either in a context diff listing header or with anIndex: line. If you are patching something in a subdirectory, be sure to tell the patch user to specify a-p flag as needed.
  • You can create a file by sending out a diff listing that compares a null file to the file you want to create. However, this only works if the file you want to create does not already exist in the target directory.
  • While you may be able to put many diff listings into one file, it is advisable to group related patches into separate files.
  • The patch command cannot tell if the line numbers are incorrect in an ed script, and can only detect bad line numbers in a normal diff listing when it finds a change or a delete command. A context diff listing using a fuzz factor of 3 may have the same line-number problem. Until a suitable interactive interface is added, use a context diff listing in such cases to check the changes for accuracy. Compilation without errors usually means that the patch worked, but it is not an infallible indicator.
  • The results of the patch command are guaranteed only when the patch is applied to exactly the same version of the file from which the patch was generated.
  • If the code has been duplicated, for example:

    #ifdef
    ... NEWCODE
    #else
    ... OLDCODE
    # endif
    the patch command is incapable of patching both versions. If thepatch command succeeds, it may have patched the wrong version and return a successful exit status.

Flags

-b Saves a copy of each modified file before the differences are applied. The copied original is filed with the same name and the suffix.orig. If a file by that name already exists, it is overwritten. If multiple patches are applied to the same file, only one copy is made of the original file at the time of the first patch. If the-o OutFile flag is also specified, the .orig file is not created. But if the specified out file already exists,OutFile.orig is created.
-B Prefix Specifies a prefix to the backup file name. This flag only works in conjunction with the-b flag.
-c Interprets the patch file as a context diff listing (the output of the diff -c or diff -C command). This flag cannot be used with the-e or -n flag.
-d Directory Changes the current directory to the specified directory before processing.
-D Define Marks changes with the following C preprocessor construct:

#ifdef Define
... (NEWCODE)
#else
... (OLDCODE)
#endif /* Define */

The Define variable is used as the differentiating symbol. This flag only works when the normal or context form of diff listing is used as a patch file.

-e Interprets the patch file as an ed editor script. This flag cannot be used with the-c or -n flag.
-f Suppresses queries to the user. To suppress commentary, use the -s flag.
-F Number Sets the maximum fuzz factor. This flag applies to context diff listings only and causes thepatch command to ignore the specified number of lines when determining where to install a hunk. If the-F flag is not specified, the default fuzz factor is 2. The factor may not be set to more than the number of lines of content in the context diff listing (ordinarily 3).
Note: A larger fuzz factor increases the odds of a faulty patch.
-i PatchFile Reads the patch information from the specified file, rather than from standard input.
-l (lowercase L) Causes any sequence of blank characters in the diff listing script to match any sequence of blank characters in the input file. Other characters are matched exactly.
-n Interprets the script as a normal diff listing. This flag cannot be used with the-c or -e flag.
-N Ignores patches where the differences have already been applied to the file. By default, already-applied patches are rejected.
-o OutFile Copies the files to be patched, applies the changes, then writes the modified version to the specified output file. Multiple patches for a single file are applied to the intermediate versions of the file created by any previous patches. Therefore, multiple patches result in multiple, concatenated versions of the output file.
-p Number Sets the path name strip count, which controls how path names found in the patch file are treated. This flag is useful if you keep your files in a directory different from the specified path. The strip count specifies how many slashes are stripped from the front of the path name. Any intervening directory names are also stripped. For example, assume a patch file specified/u/leon/src/blurf1/blurf1.c:
  • -p 0 leaves the entire path name unmodified.
  • -p 1 removes the leading slash, leaving u/leon/src/blurf1/blurf1.c.
  • -p 4 removes four slashes and three directories, leaving blurf1/blurf1.c.

If the -p flag is not specified, only the base name (the final path name component) is used. This flag works only when theFile parameter is not specified.

-r RejectFile Overrides the default reject file name. The default reject file name is formed by appending the suffix.rej to the original file name.
-R Reverses the sense of the patch script. For example, if the diff listing was created from new version to old version, using the-R flag causes the patch command to reverse each portion of the script before applying it. Rejected differences are saved in swapped format. The-R flag cannot be used with ed scripts, because there is too little information to reconstruct the reverse operation. If the-R flag is not specified, the patch command attempts to apply each portion in its reversed sense as well as in its normal sense, until a portion of the patch file is successfully applied. If the attempt is successful, the user is prompted to determine if the -R flag should be set.
Note: This method cannot detect a reversed patch if used with a normal diff listing where the first command is an append (that is, would have been a delete). Appends always succeed because a null context matches anywhere. Fortunately, most patches add or change lines rather than delete lines. Therefore most reversed normal diff listings begin with a delete, causing a failure and triggering heuristics.
-s Patches silently unless an error occurs.
-v Prints the revision header and patch level. If the -v flag is used with other flags, the other flags are ignored.
-x Number Sets internal debugging flags. This flag is only for patch command developers.

Exit Status

The following exit values are returned:

0 Successful completion.
1 An error occurred.

Examples

  1. To apply diff listings in the difflisting file to the prog.c file, enter:
    patch -i difflisting prog.c
  2. To save the original version of the prog.c file, enter:
    patch -b -i difflisting prog.c
    This applies changes to prog.c and saves the original contents of prog.c in the file prog.c.orig.
  3. To patch the prog.c file without altering the original version, enter:
    patch -i difflisting -o prog.new prog.c
    This uses prog.c as a source file, but the changed version is written to a file namedprog.new.

Files

/usr/bin/patch Contains the patch command.

Related Information

The diff command, ed command.