站点图标 Linux命令大全(手册)

patch

patch

Linux patch命令用于修补文件。

patch指令让用户利用设置修补文件的方式,修改,更新原始文件。倘若一次仅修改一个文件,可直接在指令列中下达指令依序执行。如果配合修补文件的方式则能一次修补大批文件,这也是Linux系统核心的升级方法之一。

语法

patch [-bceEflnNRstTuvZ][-B <备份字首字符串>][-d <工作目录>][-D <标示符号>][-F <监别列数>][-g <控制数值>][-i <修补文件>][-o <输出文件>][-p <剥离层级>][-r <拒绝文件>][-V <备份方式>][-Y <备份字首字符串>][-z <备份字尾字符串>][--backup-if -mismatch][--binary][--help][--nobackup-if-mismatch][--verbose][原始文件 <修补文件>]  path [-p <剥离层级>] < [修补文件]

参数

实例

使用patch指令将文件”testfile1″升级,其升级补丁文件为”testfile.patch”,输入如下命令:

$ patch -p0 testfile1 testfile.patch    #使用补丁程序升级文件 

使用该命令前,可以先使用指令”cat”查看”testfile1″的内容。在需要修改升级的文件与原文件之间使用指令”diff”比较可以生成补丁文件。具体操作如下所示:

$ cat testfile1                 #查看testfile1的内容  
Hello,This is the firstfile!  
$ cat testfile2                 #查看testfile2的内容  
Hello,Thisisthesecondfile!  
$ diff testfile1 testfile2          #比较两个文件  
1c1  
<Hello,Thisisthefirstfile!  
---  
>Hello,Thisisthesecondfile!  
#将比较结果保存到tetsfile.patch文件  
$ diff testfile1 testfile2>testfile.patch     
$ cat testfile.patch                #查看补丁包的内容  
1c1  
<Hello,Thisisthefirstfile!  
---  
>Hello,Thisisthesecondfile!  
#使用补丁包升级testfile1文件  
$ patch -p0 testfile1 testfile.patch      
patching file testfile1  
$cat testfile1                  #再次查看testfile1的内容  
#testfile1文件被修改为与testfile2一样的内容  
Hello,This is the secondfile!

注意:上述命令代码中,”$ diff testfile1 testfile2>testfile. patch”所使用的操作符”>”表示将该操作符左边的文件数据写入到右边所指向的文件中。在这里,即是指将两个文件比较后的结果写入到文件”testfile.patch”中。

退出移动版