Linux "patch" Command Line Options and Examples
apply a diff file to an original

patch takes a patch file patchfile containing a difference listing produced by the diff program and applies those differences to one or more original files, producing patched versions. Normally the patched versions are put in place of the originals. Backups can be made; see the -b or --backup option.


Usage:

patch [options] [originalfile [patchfile]]


    but usually just


    patch -pnum <patchfile






Command Line Options:

-b
Make backup files. That is, when patching a file, rename or copy the original instead of removing it. Seethe -V or --version-control option for details about how backup file names are determined.
patch -b ...
--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.
patch --backup-if-mismatch ...
--no-backup-if-mismatch
Do not back up a file if the patch does not match the file exactly and if backups are not otherwiserequested. This is the default if patch is conforming to POSIX.
patch --no-backup-if-mismatch ...
-B
Use the simple method to determine backup file names (see the -V method or --version-control methodoption), 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.
patch -B ...
--binary
Write all files in binary mode, except for standard output and /dev/tty. When reading, disable the heuris‐tic for transforming CRLF line endings into LF line endings. This option is needed on POSIX systems whenapplying patches generated on non-POSIX systems to non-POSIX files. (On POSIX systems, file reads andwrites never transform line endings. On Windows, reads and writes do transform line endings by default, andpatches should be generated by diff --binary when line endings are significant.)
patch --binary ...
-c
Interpret the patch file as a ordinary context diff.
patch -c ...
-d
Change to the directory dir immediately, before doing anything else.
patch -d ...
-D
Use the #ifdef ... #endif construct to mark changes, with define as the differentiating symbol.
patch -D ...
--dry-run
Print the results of applying the patches without actually changing any files.
patch --dry-run ...
-e
Interpret the patch file as an ed script.
patch -e ...
-E
Remove output files that are empty after the patches have been applied. Normally this option is unneces‐sary, since patch can examine the time stamps on the header to determine whether a file should exist afterpatching. However, if the input is not a context diff or if patch is conforming to POSIX, patch does notremove empty patched files unless this option is given. When patch removes a file, it also attempts toremove any empty ancestor directories.
patch -E ...
-f
Assume that the user knows exactly what he or she is doing, and do not ask any questions. Skip patcheswhose headers do not say which file is to be patched; patch files even though they have the wrong versionfor the Prereq: line in the patch; and assume that patches are not reversed even if they look like theyare. This option does not suppress commentary; use -s for that.
patch -f ...
-F
Set the maximum fuzz factor. This option only applies to diffs that have context, and causes patch toignore up to that many lines of context in looking for places to install a hunk. Note that a larger fuzzfactor increases the odds of a faulty patch. The default fuzz factor is 2. A fuzz factor greater than orequal to the number of lines of context in the context diff, ordinarily 3, ignores all context.
patch -F ...
-g
This option controls patch's actions when a file is under RCS or SCCS control, and does not exist or isread-only and matches the default version, or when a file is under ClearCase or Perforce control and doesnot exist. If num is positive, patch gets (or checks out) the file from the revision control system; ifzero, patch ignores RCS, ClearCase, Perforce, and SCCS and does not get the file; and if negative, patchasks the user whether to get the file. The default value of this option is given by the value of thePATCH_GET environment variable if it is set; if not, the default value is zero.
patch -g ...
--help
Print a summary of options and exit.
patch --help ...
-i
Read the patch from patchfile. If patchfile is -, read from standard input, the default.
patch -i ...
-l
Match patterns loosely, in case tabs or spaces have been munged in your files. Any sequence of one or moreblanks in the patch file matches any sequence in the original file, and sequences of blanks at the ends oflines are ignored. Normal characters must still match exactly. Each line of the context must still matcha line in the original file.
patch -l ...
--merge
Merge a patch file into the original files similar to diff3(1) or merge(1). If a conflict is found, patchoutputs a warning and brackets the conflict with <<<<<<< and >>>>>>> lines. A typical conflict will looklike this:<<<<<<<lines from the original file|||||||original lines from the patch=======new lines from the patch>>>>>>>The optional argument of --merge determines the output format for conflicts: the diff3 format shows the||||||| section with the original lines from the patch; in the merge format, this section is missing. Themerge format is the default.This option implies --forward and does not take the --fuzz=num option into account.
patch --merge ...
-n
Interpret the patch file as a normal diff.
patch -n ...
-N
When a patch does not apply, patch usually checks if the patch looks like it has been applied already bytrying to reverse-apply the first hunk. The --forward option prevents that. See also -R.
patch -N ...
-o
Send output to outfile instead of patching files in place. Do not use this option if outfile is one of thefiles to be patched. When outfile is -, send output to standard output, and send any messages that wouldusually go to standard output to standard error.
patch -o ...
-pnum
Strip the smallest prefix containing num leading slashes from each file name found in the patch file. Asequence of one or more adjacent slashes is counted as a single slash. This controls how file names foundin the patch file are treated, in case you keep your files in a different directory than the person whosent out the patch. For example, supposing the file name in the patch file was/u/howard/src/blurfl/blurfl.csetting -p0 gives the entire file name unmodified, -p1 givesu/howard/src/blurfl/blurfl.cwithout the leading slash, -p4 givesblurfl/blurfl.cand not specifying -p at all just gives you blurfl.c. Whatever you end up with is looked for either in thecurrent directory, or the directory specified by the -d option.
patch -pnum ...
--posix
Conform more strictly to the POSIX standard, as follows.· Take the first existing file from the list (old, new, index) when intuiting file names from diff head‐ers.· Do not remove files that are empty after patching.· Do not ask whether to get files from RCS, ClearCase, Perforce, or SCCS.· Require that all options precede the files in the command line.· Do not backup files when there is a mismatch.
patch --posix ...
--quoting-style
Use style word to quote output names. The word should be one of the following:literalOutput names as-is.shell Quote names for the shell if they contain shell metacharacters or would cause ambiguous output.shell-alwaysQuote names for the shell, even if they would normally not require quoting.c Quote names as for a C language string.escape Quote as with c except omit the surrounding double-quote characters.You can specify the default value of the --quoting-style option with the environment variable QUOT‐ING_STYLE. If that environment variable is not set, the default value is shell.
patch --quoting-style ...
-r
Put rejects into rejectfile instead of the default .rej file. When rejectfile is -, discard rejects.
patch -r ...
-R
Assume that this patch was created with the old and new files swapped. (Yes, I'm afraid that does happenoccasionally, human nature being what it is.) patch attempts to swap each hunk around before applying it.Rejects come out in the swapped format. The -R option does not work with ed diff scripts because there istoo little information to reconstruct the reverse operation.If the first hunk of a patch fails, patch reverses the hunk to see if it can be applied that way. If itcan, you are asked if you want to have the -R option set. If it can't, the patch continues to be appliednormally. (Note: this method cannot detect a reversed patch if it is a normal diff and if the first com‐mand is an append (i.e. it should have been a delete) since appends always succeed, due to the fact that anull context matches anywhere. Luckily, most patches add or change lines rather than delete them, so mostreversed normal diffs begin with a delete, which fails, triggering the heuristic.)
patch -R ...
--read-only
Behave as requested when trying to modify a read-only file: ignore the potential problem, warn about it(the default), or fail.
patch --read-only ...
--reject-format
Produce reject files in the specified format (either context or unified). Without this option, rejectedhunks come out in unified diff format if the input patch was of that format, otherwise in ordinary contextdiff form.
patch --reject-format ...
-s
Work silently, unless an error occurs.
patch -s ...
--follow-symlinks
When looking for input files, follow symbolic links. Replaces the symbolic links, instead of modifying thefiles the symbolic links point to. Git-style patches to symbolic links will no longer apply. This optionexists for backwards compatibility with previous versions of patch; its use is discouraged.
patch --follow-symlinks ...
-t
Suppress questions like -f, but make some different assumptions: skip patches whose headers do not containfile names (the same as -f); skip patches for which the file has the wrong version for the Prereq: line inthe patch; and assume that patches are reversed if they look like they are.
patch -t ...
-T
Set the modification and access times of patched files from time stamps given in context diff headers.Unless specified in the time stamps, assume that the context diff headers use local time.Use of this option with time stamps that do not include time zones is not recommended, because patchesusing local time cannot easily be used by people in other time zones, and because local time stamps areambiguous when local clocks move backwards during daylight-saving time adjustments. Make sure that timestamps include time zones, or generate patches with UTC and use the -Z or --set-utc option instead.
patch -T ...
-u
Interpret the patch file as a unified context diff.
patch -u ...
-v
Print out patch's revision header and patch level, and exit.
patch -v ...
-V
Use method to determine backup file names. The method can also be given by the PATCH_VERSION_CONTROL (or,if that's not set, the VERSION_CONTROL) environment variable, which is overridden by this option. Themethod does not affect whether backup files are made; it affects only the names of any backup files thatare made.The value of method is like the GNU Emacs `version-control' variable; patch also recognizes synonyms thatare more descriptive. The valid values for method are (unique abbreviations are accepted):existing or nilMake numbered backups of files that already have them, otherwise simple backups. This is the default.numbered or tMake numbered backups. The numbered backup file name for F is F.~N~ where N is the version number.simple or neverMake simple backups. The -B or --prefix, -Y or --basename-prefix, and -z or --suffix options specifythe simple backup file name. If none of these options are given, then a simple backup suffix is used;it is the value of the SIMPLE_BACKUP_SUFFIX environment variable if set, and is .orig otherwise.With numbered or simple backups, if the backup file name is too long, the backup suffix ~ is used instead;if even appending ~ would make the name too long, then ~ replaces the last character of the file name.
patch -V ...
--verbose
Output extra information about the work being done.
patch --verbose ...
-x
Set internal debugging flags of interest only to patch patchers.
patch -x ...
-Y
Use the simple method to determine backup file names (see the -V method or --version-control methodoption), and prefix pref to the basename of a file name when generating its backup file name. For example,with -Y .del/ the simple backup file name for src/patch/util.c is src/patch/.del/util.c.
patch -Y ...
-z
Use the simple method to determine backup file names (see the -V method or --version-control methodoption), and use suffix as the suffix. For example, with -z - the backup file name for src/patch/util.c issrc/patch/util.c-.
patch -z ...
-Z
Set the modification and access times of patched files from time stamps given in context diff headers.Unless specified in the time stamps, assume that the context diff headers use Coordinated Universal Time(UTC, often known as GMT). Also see the -T or --set-time option.The -Z or --set-utc and -T or --set-time options normally refrain from setting a file's time if the file'soriginal time does not match the time given in the patch header, or if its contents do not match the patchexactly. However, if the -f or --force option is given, the file time is set regardless.Due to the limitations of diff output format, these options cannot update the times of files whose contentshave not changed. Also, if you use these options, you should remove (e.g. with make clean) all files thatdepend on the patched files, so that later invocations of make do not get confused by the patched files'times.ENVIRONMENTPATCH_GETThis specifies whether patch gets missing or read-only files from RCS, ClearCase, Perforce, or SCCS bydefault; see the -g or --get option.POSIXLY_CORRECTIf set, patch conforms more strictly to the POSIX standard by default: see the --posix option.QUOTING_STYLEDefault value of the --quoting-style option.SIMPLE_BACKUP_SUFFIXExtension to use for simple backup file names instead of .orig.TMPDIR, TMP, TEMPDirectory to put temporary files in; patch uses the first environment variable in this list that is set.If none are set, the default is system-dependent; it is normally /tmp on Unix hosts.VERSION_CONTROL or PATCH_VERSION_CONTROLSelects version control style; see the -v or --version-control option.FILES$TMPDIR/p*temporary files/dev/ttycontrolling terminal; used to get answers to questions asked of the user
patch -Z ...