Linux patch command - apply a diff file

Linux patch command: apply a diff file

Linux patch command Function Description

Using the patch command you can apply a patch file generated with the diff program that contains a list of differences, thus allowing for an upgrade.

Linux patch command Syntax

patch [Option] [original File [patch File]]
patch [Option] <[patch File]

The meaning of each option in the command is shown in the following table.

Option Description
-o <File> Send output to outfile instead of patching files in place. Do not use this option if outfile is one of the files to be patched. When outfile is -, send output to standard output, and send any messages that would usually go to standard output to standard error.
-d <Directory> Change to the directory dir immediately, before doing anything else.
--binary Read and write data in binary mode
-l Match patterns loosely, in case tabs or spaces have been munged in your files.
-i <patchfile> Read the patch from patchfile. If patchfile is -, read from standard input, the default.
-V <Method> Use method to determine backup file names.
-r <File> Put rejects into rejectfile instead of the default .rej file. When rejectfile is -, discard rejects.
-D <Name> Use the #ifdef ... #endif construct to mark changes, with define as the differentiating symbol.
-z <suffix> Use the simple method to determine backup file names (see the -V method or –version-control method option), and use suffix as the suffix.
-Y <prefix> Use the simple method to determine backup file names (see the -V method or –version-control method option), and prefix pref to the basename of a file name when generating its backup file name.
-m Do not create reject files, use conflict flags to merge
-B <prefix> Use the simple method to determine backup file names (see the -V method or –version-control method option), and append pref to a file name when generating its backup file name. For example, with -B /junk/ the simple backup file name for src/patch/util.c is /junk/src/patch/util.c.
-E Remove output files that are empty after the patches have been applied.
-T Set the modification and access times of patched files from time stamps given in context diff headers.
-Z Set the modification and access times of patched files from time stamps given in context diff headers.
-b Make backup files. That is, when patching a file, rename or copy the original instead of removing it. When backing up a file that does not exist, an empty, unreadable backup file is created as a placeholder to represent the nonexistent file. See the -V or –version-control option for details about how backup file names are determined.
--backup-if-mismatch Back up a file if the patch does not match the file exactly and if backups are not otherwise requested. This is the default unless patch is conforming to POSIX.
-e Interpret the patch file as an ed script.
-u Interpret the patch file as a unified context diff.
-n Interpret the patch file as a normal diff.
-t Suppress questions like -f, but make some different assumptions: skip patches whose headers do not contain file names (the same as -f); skip patches for which the file has the wrong version for the Prereq: line in the patch; and assume that patches are reversed if they look like they are.
-s Work silently, unless an error occurs.
--verbose Output extra information about the work being done.
--dry-run Print the results of applying the patches without actually changing any files.

Linux patch command Example

Apply patch files

[root@rhel ~]# cat /root/a1
aaa
bbb
[root@rhel ~]# cat /root/a2
aaa
ccc
[root@rhel ~]# diff -ruN /root/a1 /root/a2 >/root/a.diff
// Compare the differences between the /root/a1 and /root/a2 files and generate the diff file /root/a.diff

[root@rhel ~]# patch -p1</root/a.diff
can't find file to patch at input line 3
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|---/root/a12014-07-07 14:37:46.137157231+0800
|+++/root/a22014-07-07 14:37:52.759156785+0800
--------------------------
File to patch: /root/a2                 // Type /root/a2
patching file /root/a2
Reversed(or previously applied)patch detected! Assume-R?[n]y             // Enter y to confirm
// This updates the /root/a2 file with the contents of the /root/a1 file
[root@rhel ~]# cat /root/a2
aaa
bbb

Output:

Linux patch command

Like(1)

Related

Linux Login Logout Command
Linux login commandLinux logout commandLinux nologin commandLinux exit commandLinux sulogin commandLinux rlogin commandLinux poweroff commandLinux ctrlaltdel CommandLinux shutdown commandLinux halt commandLinux reboot commandLinux init commandLinux runlevel commandLinux telinit command
Linux File Management Command
Linux cat commandLinux tac commandLinux nl commandLinux more commandLinux less commandLinux head commandLinux tail commandLinux rev commandLinux fold commandLinux fmt commandLinux expand commandLinux pr commandLinux sort commandLinux uniq commandLinux cut commandLinux comm commandLinux diff commandLinux join commandLinux diff3 commandLinux cmp commandLinux colrm commandLinux paste commandLinux mkdir commandLinux tr commandLinux split commandLinux csplit commandLinux tee commandLinux unexpand commandLinux patch commandLinux awk commandLinux sed commandLinux od commandLinux pwd commandLinux cd commandLinux ls commandLinux dir commandLinux dirs commandLinux touch commandLinux rmdir commandLinux cp commandLinux mv commandLinux rm commandLinux install commandLinux tmpwatch commandLinux file commandLinux du commandLinux wc commandLinux tree commandLinux cksum commandLinux md5sum commandLinux sum commandLinux dirname commandLinux mkfifo Command
Cron Expressions
Cron Expression to Run Every Day at 12 PMUnderstanding Vue Cron ExpressionsUnderstanding JS Cron ExpressionsA Comprehensive Guide to Cron Expressions for Scheduled TasksUnderstanding Linux Cron ExpressionsUnderstanding Quartz Cron ExpressionsCron ExpressionCron Time ExpressionCron Expression ParsingCron Expression: Executing a Task Every SecondCron Expression for Every Minute ExecutionCron Expression to Execute Every 10 MinutesCron Expression: Executing Every HourCron Expression to Execute Once a YearCron Expression: How to Schedule a Task to Run Daily at Midnight?