Patch Command

The patch command allows the user to modify and update original files by applying patch files. When making changes to just one file, the command can be executed directly in the command line. However, when used with patch files, it can patch a large number of files at once, making it one of the core upgrading methods for the Linux system.

Syntax

patch [OPTION]... [ORIGFILE [PATCHFILE]]

Parameters

  • -b, --backup: Make a backup of each original file.
  • -B<pref>, --prefix=<pref>: Set the prefix string to be attached to the file name when making backups. This string can be a pathname.
  • -c, --context: Interpret the patch data as context differences.
  • -d<dir>, --directory=<dir>: Set the working directory.
  • -D<define>, --ifdef=<define>: Mark the changes with the specified symbol.
  • -e, --ed: Interpret the patch data as a descriptive file for the ed command.
  • -E, --remove-empty-files: Remove the patched file if it becomes empty after the patching process.
  • -f, --force: Similar to the -t option, but assumes that the patch data is for a new version.
  • -F<num >, --fuzz<num >: Set the maximum fuzz factor.
  • -g<num>, --get=<num>: Control patching jobs using RSC or SCCS.
  • -i<patchfile>, --input=<patchfile>: Read the specified patch file.
  • -l, --ignore-whitespace: Ignore differences in tab and space characters between the patch data and the input data.
  • -n, --normal: Interpret the patch data as normal differences.
  • -N, --forward: Ignore patch data that is older than the original file version or has been used before.
  • -o<outfile>, --output=<outfile>: Set the name of the output file, where the patched file will be stored.
  • -p<num>, --strip=<num>: Strip the specified number of leading path components.
  • -f<rejectfile>, --reject-file=<rejectfile>: Set the file name to save information about rejected patches, with the default name .rej.
  • -R, --reverse: Assume that the patch data is generated by swapping the new and old files.
  • -s, --quiet or --silent: Suppress normal output and only show errors.
  • -t, --batch: Automatically skip errors without asking any questions.
  • -T, --set-time: Similar to the -Z option, but uses local time as the primary time.
  • -u, --unified: Interpret the patch data as unified differences.
  • -v, --version: Display version information.
  • -V<method>, --version-control=<method>: When using the -b option to back up target files, a suffix is added to the backup file name. This suffix can be changed with the -z option, and different backup methods can be specified with the -V option, resulting in different suffixes for backup files.
  • -Y<pref>, --basename-prefix=--<pref>: Set the prefix string to be attached to the base name of the file when making backups.
  • -z<suffix>, --suffix=<suffix>: Similar to the -B option, but when used, the path and file name used in the patching process, such as src/linux/fs/super.c, will result in the file super.c being backed up in the /src/linux/fs/backup directory when the backup/ string is added.
  • -Z, --set-utc: Set the access and modification times of the patched file to UTC.
  • --backup-if-mismatch: Back up the file only when the patch data does not completely match and no deliberate backup is specified.
  • --binary: Read and write data in binary mode, bypassing the standard output device.
  • --help: Display online help.
  • --nobackup-if-mismatch: Do not back up the file when the patch data does not completely match and no deliberate backup is specified.
  • --verbose: Display detailed output of the command execution.

Example

Upgrade the file /tmp/file2.txt using the patch file /tmp/file.patch.

# diff /tmp/file2.txt /tmp/file3.txt > /tmp/file.patch # Generate the patch file
patch /tmp/file2.txt /tmp/file.patch

Daily Question

https://github.com/WindrunnerMax/EveryDay

References

https://man.linuxde.net/patch https://www.runoob.com/linux/linux-comm-patch.html https://www.tutorialspoint.com/unix_commands/patch.htm