Linux patch command
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:
- -b or --backup Back up each original file.
- -B
or --prefix= Set a prefix string to be prepended to file names when backing up; the string can be a path name. - -c or --context Interpret the patch data as contextual differences.
- -d
or --directory= Set the working directory. - -D
or --ifdef= Mark the changed places with the specified symbol. - -e or --ed Interpret the patch data as a script usable by the ed command.
- -E or --remove-empty-files If a patched output file becomes empty, remove that file.
- -f or --force This option has an effect similar to the "-t" parameter, but it assumes the patch data is for a new version.
- -F
or --fuzz sets the maximum fuzz factor. - -g
or --get= controls the patch operation using RSC or SCCS. - -i
or --input= reads the specified patch file. - -l or --ignore-whitespace ignores tabs and space characters in patch data and input data.
- -n or --normal interprets patch data as a normal diff.
- -N or --forward ignores patches when the patch data is older than the original file version, or when that version of the patch data has already been applied.
- -o
- -p
or --strip= sets how many levels of path names to strip. - -f
or --reject-file= sets the file name that stores information about rejected patches; the default file name is .rej. - -R or --reverse assumes the patch data was generated by swapping the old and new files.
- -s or --quiet or --silent does not display the command execution process unless an error occurs.
- -t or --batch automatically skips errors without asking any questions.
- -T or --set-time similar to the "-Z" option, but uses local time.
- -u or --unified interprets patch data as a unified diff.
- -v or --version Display version information.
- -V
or --version-control= after backing up the target file with the "-b" option, a backup string is appended to the backup file name; this string can be changed not only with the "-z" option, but also produces different suffixes when different backup methods are specified with the "-V" option. - -Y
or --basename-prefix=-- sets the prefix string appended to the beginning of a file's base name when the file is backed up. - -z
or --suffix= similar to the "-B" option, the difference is that if the path and file name used by the patch operation is 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 sets the modification and access times of patched files to UTC.
- --backup-if-mismatch backs up a file only when the patch data does not match exactly and no backup was explicitly requested.
- --binary reads and writes data in binary mode, without going through standard output.
- --help Online help.
- --nobackup-if-mismatch does not back up a file when the patch data does not match exactly and no backup was explicitly requested.
- --verbose displays the command execution process in detail.
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