Linux patch Command
The Linux patch command is used to apply patches to files.
The patch command allows users to modify and update original files using patch files. If only one file is to be patched, commands can be executed sequentially directly in the command line. If patch files are used, multiple files can be patched at once, which is also one of the methods for upgrading the Linux kernel.
Syntax
patch [-bceEflnNRstTuvZ][-B <backup prefix string>][-d <working directory>][-D <symbol>][-F <fuzz factor>][-g <control value>][-i <patch file>][-o <output file>][-p <strip level>][-r <reject file>][-V <backup method>][-Y <backup prefix string>][-z <backup suffix string>][--backup-if-mismatch][--binary][--help][--nobackup-if-mismatch][--verbose][original file <patch file>] or patch [-p <strip level>] < [patch file]
Parameters:
- -b or --backup: Backup each original file.
- -B<backup prefix string> or --prefix=<backup prefix string>: Set the prefix string prepended to file names when backing up files. The string can be a path name.
- -c or --context: Interpret the patch data as context differences.
- -d<working directory> or --directory=<working directory>: Set the working directory.
- -D<symbol> or --ifdef=<symbol>: Use the specified symbol to mark changes.
- -e or --ed: Interpret the patch data as ed script.
- -E or --remove-empty-files: Remove output files that are empty after patching.
- -f or --force: This parameter is similar to the "-t" parameter but assumes the patch data is for a newer version.
- -F<fuzz factor> or --fuzz<fuzz factor>: Set the maximum fuzz factor.
- -g<control value> or --get=<control value>: Control patch operations using RSC or SCCS.
- -i<patch file> or --input=<patch file>: Read the specified patch file.
- -l or --ignore-whitespace: Ignore tabs and spaces in the patch data and input data.
- -n or --normal: Interpret the patch data as normal differences.
- -N or --forward: Ignore patches that are older than the original file or have already been applied.
- -o<output file> or --output=<output file>: Set the name of the output file, which will contain the patched content.
- -p<strip level> or --strip=<strip level>: Set the number of directory levels to strip.
- -r<reject file> or --reject-file=<reject file>: Set the name of the file to store reject information, default is .rej.
- -R or --reverse: Assume the patch was created by swapping old and new files.
- -s or --quiet or --silent: Do not display command execution process unless there is an error.
- -t or --batch: Automatically skip errors without asking questions.
- -T or --set-time: This parameter is similar to the "-Z" parameter but uses local time.
- -u or --unified: Interpret the 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" parameter, a backup string is appended to the file name. This string can be changed with the "-z" parameter and varies with the backup method specified with the "-V" parameter.
- -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 parameter is similar to the "-B" parameter, but the patch operation uses paths and file names like src/linux/fs/super.c, and adding "backup/" will backup super.c in the /src/linux/fs/backup directory.
- -Z or --set-utc: Set the access and modification times of patched files to UTC.
- --backup-if-mismatch: Backup the file if the patch data does not fully match and no backup is explicitly specified.
- --binary: Read and write data in binary mode without using standard output.
- --help: Online help.
- --nobackup-if-mismatch: Do not backup the file if the patch data does not fully match and no backup is explicitly specified.
- --verbose: Display detailed execution process of the command.
Example
Use the patch command to upgrade the file "testfile1" with the upgrade patch file "testfile.patch" by entering the following command:
$ patch -p0 testfile1 testfile.patch # Upgrade the file using the patch program
Before using this command, you can view the contents of "testfile1" using the "cat" command. To generate a patch file by comparing the file to be modified with the original file, use the "diff" command. The specific steps are as follows:
$ cat testfile1 # View the contents of testfile1
Hello, This is the first file!
$ cat testfile2 # View the contents of testfile2
Hello, This is the second file!
$ diff testfile1 testfile2 # Compare the two files
1c1
< Hello, This is the first file!
---
> Hello, This is the second file!
# Save the comparison result to the testfile.patch file
$ diff testfile1 testfile2 > testfile.patch
$ cat testfile.patch # View the contents of the patch file
1c1
< Hello, This is the first file!
---
> Hello, This is the second file!
# Use the patch file to upgrade testfile1
$ patch -p0 testfile1 testfile.patch
patching file testfile1
$ cat testfile1 # View the contents of testfile1 again
# The testfile1 file is modified to have the same content as testfile2
Hello, This is the second file!
Note: In the command code above, "$ diff testfile1 testfile2 > testfile.patch" uses the operator ">" to write the data from the file on the left to the file on the right. Here, it means writing the result of the comparison between the two files into the "testfile.patch" file.