Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux patch command

Linux patch command

Linux Command Reference Linux Command Reference

The Linux patch command is used to patch files.

The patch command lets users modify and update original files by applying patch files. If you are modifying only one file at a time, you can issue commands directly on the command line in sequence. If you use patch files together, you can patch large numbers of files at once, which is one of the methods for upgrading the Linux kernel.

Syntax

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 <剥离层级>] < [修补文件]

Parameters:

Example

Use the patch command to update the file "testfile1", with the update patch file "testfile.patch", by entering the following command:



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

Before using this command, you can use the "cat" command to view the content of "testfile1". Using the "diff" command to compare the file to be modified with the original file can generate a patch file. The specific operations are as follows:



$ cat testfile1                 #查看testfile1的内容  

Hello,This is the firstfile!  

$ cat testfile2                 #查看testfile2的内容  

Hello,Thisisthesecondfile!  

$ diff testfile1 testfile2          #比较两个文件  

1c1  

Hello,Thisisthesecondfile!  

#Save comparison results to testfile.patch

$ diff testfile1 testfile2>testfile.patch     

$ cat testfile.patch                #查看补丁包的内容  

1c1  

Hello,Thisisthesecondfile!  

#Upgrade testfile1 with patch package

$ patch -p0 testfile1 testfile.patch      

patching file testfile1  

$cat testfile1                  #再次查看testfile1的内容  

#testfile1 file is modified to have the same content as testfile2

Hello,This is the secondfile!   

Note: in the command code above, the operator ">" used in "$ diff testfile1 testfile2>testfile. patch" means writing the file data on the left side of the operator to the file pointed to on the right side. Here, it means writing the result of comparing the two files into the file "testfile.patch".

Linux Command Reference Linux Command Reference