1. 程式人生 > >打補丁patch 命令使用

打補丁patch 命令使用

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命令應用

  1. 1.1 用途
  2. 1.2 語法
  3. 1.3 描述

2檔名

3應用程式

4準備補丁

  1. 4.1 原則
  2. 4.2 標誌
  3. 4.3 示例
  4. 4.4 檔案

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.

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 ]

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 (or diff 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, the patch 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. The patch 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, leaving blurfl/blurfl.c.

Not specifying the -p flag causes the patch command to use the base name. In the above example, that would be blurfl.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 the diff -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 the patch command would still work. In such a case, if the entire diff listing is indented by a consistent amount, the patch 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, the patch 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, the patch 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, the patch command interprets information within each hunk as a context difference, an ed editor difference, or a normal difference, respectively. Otherwise, the patch 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, the patch 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, the patch 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, the patch 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, the patch 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 a Prereq: 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 an Index: 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 the patch 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 the patch 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 the File 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 named prog.new.

Files

/usr/bin/patch Contains the patch command.

Related Information

The diff command, ed command.