Linux patch command
The Linux patch command is used to patch files.
The patch command allows users to modify and update original files by using patch files. If only one file is modified at a time, commands can be issued directly in the command line and executed sequentially. If combined with patch files, a large number of files can be patched at once; this is also one of the methods for upgrading the Linux system 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:
- -b or --backup: Back up each original file.
- -B<backup prefix string> or --prefix=<backup prefix string>: Set the prefix string prepended to filenames when backing up files; this string can be a path name.
- -c or --context: Interpret patch data as contextual differences.
- -d<working directory> or --directory=<working directory>: Set the working directory.
- -D<symbol> or --ifdef=<symbol>: Mark the changed places with the specified symbol.
- -e or --ed: Interpret patch data as a script usable by the ed command.
- -E or --remove-empty-files: If the output file after patching is blank, remove that file.
- -f or --force: This option is similar to the "-t" option, but assumes the patch data is a newer version.
- -F<lines> or --fuzz=<lines>: Set the maximum number of fuzzy matching lines.
- -g<control value> or --get=<control value>: Set the patch job to be controlled by RSC or SCCS.
- -i<patch file> or --input=<patch file>: Read the specified patch file.
- -l or --ignore-whitespace: Ignore tabs and spaces in patch data and input data.
- -n or --normal: Interpret patch data as normal differences.
- -N or --forward: Ignore patch data that is older than the original file, or if this version of the patch data has already been used.
- -o<output file> or --output=<output file>: Set the name of the output file; patched files will be stored under this name.
- -p<strip level> or --strip=<strip level>: Set how many levels of path names to strip.
- -f<reject file> or --reject-file=<reject file>: Set the file name to save rejection-related patch information; the default file name is .rej.
- -R or --reverse: Assume the patch data was generated by swapping the positions of new and old files.
- -s or --quiet or --silent: Do not display the command execution process unless an error occurs.
- -t or --batch: Automatically skip errors without asking any questions.
- -T or --set-time: This option is similar to the "-Z" option, but uses local time.
- -u or --unified: Interpret patch data as unified differences.
- -v or --version: Display version information.
- -V<backup method> or --version-control=<backup method>: After backing up the target file with the "-b" option, a backup string is appended to the suffix of the backup file. This string can not only be changed with the "-z" option, but when using the "-V" option to specify different backup methods, different suffix backup strings will also be produced.
- -Y<backup prefix string> or --basename-prefix=<backup prefix string>: Set the prefix string prepended to the base name of files when backing up.
- -z<backup suffix string> or --suffix=<backup suffix string>: This option is similar to the "-B" option, except that if the path and filename used in the patch job are src/linux/fs/super.c, after adding the "backup/" string, the file super.c will be backed up in the /src/linux/fs/backup directory.
- -Z or --set-utc: Set the modified and access times of patched files to UTC.
- --backup-if-mismatch: Back up files only when the patch data does not completely match and no backup file was explicitly specified.
- --binary: Read and write data in binary mode, rather than through standard output devices.
- --help: Online help.
- --nobackup-if-mismatch: Do not back up files when the patch data does not completely match and no backup file was explicitly specified.
- --verbose: Display the command execution process in detail.
Examples
Use the patch command to upgrade the file "testfile1", with the upgrade patch file being "testfile.patch". Enter the following command:
$ patch -p0 testfile1 testfile.patch #使用补丁程序升级文件
Before using this command, you can use the "cat" command to view the contents of "testfile1". You can generate a patch file by using the "diff" command to compare the file to be modified and upgraded with the original file. The specific operation is as follows:
$ cat testfile1 #查看testfile1的内容 Hello,This is the firstfile! $ cat testfile2 #查看testfile2的内容 Hello,Thisisthesecondfile! $ diff testfile1 testfile2 #比较两个文件 1c1 <Hello,Thisisthefirstfile! --- >Hello,Thisisthesecondfile! #将比较结果保存到testfile.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!
Note: In the above command code, the operator ">" used in "$ diff testfile1 testfile2>testfile. patch" means writing the file data to the left of the operator into the file pointed to on the right. Here, it means writing the comparison result of the two files into the file "testfile.patch".
Other extensions
Linux Command Encyclopedia