Linux Comm Patch
[ Linux Command Manual](#)
The Linux `patch` command is used to apply patches to files.
The `patch` command allows users to modify and update original files by applying patch files. If only one file is being modified at a time, the command can be executed sequentially directly from the command line. When used with patch files, it can apply patches to a large number of files at once, which is also one of the methods for upgrading the Linux system kernel.
### Syntax
patch or patch <
**Parameters**:
* -b or --backup: Back up every original file.
* -B or --prefix=: Set a prefix string to be prepended to the file name when backing up files. This string can be a path name.
* -c or --context: Interpret the patch data as context diffs.
* -d or --directory=: Set the working directory.
* -D or --ifdef=: Mark changes with the specified symbol.
* -e or --ed: Interpret the patch data as an ed script.
* -E or --remove-empty-files: If the output file after patching is empty, remove it.
* -f or --force: Similar to the "-t" parameter, but assumes the patch data version is the new version.
* -F or --fuzz: Set the maximum fuzz factor.
* -g or --get=: Set the control number for RSC or SCCS.
* -i or --input=: 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 diffs.
* -N or --forward: Ignore patches that are older than the original file or have already been applied.
* -o or --output=: Set the name of the output file. The patched file will be saved with this name.
* -p or --strip=: Set the number of path components to strip.
* -r or --reject-file=: Set the file name for saving rejected patch information. The default file name is .rej.
* -R or --reverse: Assume the patch data was created by reversing the old and new files.
* -s or --quiet or --silent: Do not display command execution process unless an error occurs.
* -t or --batch: Automatically skip errors without asking any questions.
* -T or --set-time: Similar to the "-Z" parameter, but uses local time.
* -u or --unified: Interpret the patch data as unified diffs.
* -v or --version: Display version information.
* -V or --version-control=: After backing up the target file with the "-b" parameter, a backup string will be appended to the backup file name. This string can be changed with the "-z" parameter, and different backup strings will be generated when different backup methods are specified with the "-V" parameter.
* -Y or --basename-prefix=: Set a prefix string to be prepended to the base name of the file when backing up files.
* -z or --suffix=: Similar to the "-B" parameter, but the difference is that if the path and file name used in 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: Set the modification and access times of the patched file to UTC.
* --backup-if-mismatch: Back up the file only if the patch data does not match exactly and no explicit backup was specified.
* --binary: Read and write data in binary mode, not through the standard output device.
* --help: Online help.
* --nobackup-if-mismatch: Do not back up the file if the patch data does not match exactly and no explicit backup was specified.
* --verbose: Display the command execution process in detail.
### Example
Use the `patch` command to upgrade the file "testfile1" with the patch file "testfile.patch". Enter the following command:
$ patch -p0 testfile1 testfile.patch #Upgrade file using patch program
Before using this command, you can first use the `cat` command to view the contents of "testfile1". The `diff` command can be used to compare the file that needs to be modified and upgraded with the original file to generate a patch file. The specific operation is as follows:
$ cat testfile1 #View contents of testfile1 Hello,This is the firstfile! $ cat testfile2 #View contents of testfile2 Hello,Thisisthesecondfile! $ diff testfile1 testfile2 #Compare two files 1c1 Hello,Thisisthesecondfile! #Save comparison results to testfile.patch file $ diff testfile1 testfile2>testfile.patch $ cat testfile.patch #View contents of patch package 1c1 Hello,Thisisthesecondfile! #Upgrade testfile1 file using patch package $ patch -p0 testfile1 testfile.patch patching file testfile1 $cat testfile1 #View contents of testfile1 again #testfile1 file is modified to the same content as testfile2 Hello,This is the secondfile!
Note: In the above command code, the operator ">" used in "$ diff testfile1 testfile2>testfile.patch" indicates writing the file data on the left side of the operator to the file pointed to on the right side. In this case, it means writing the comparison results of the two files into the file "testfile.patch".
[ Linux Command Manual](#)
YouTip