patch

From RaySoft

patch takes a patch file 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. The names of the files to be patched are usually taken from the patch file, but if there's just one file to be patched it can specified on the command line as original file.[1]

Documentation

Syntax

patch [PARAMETER ...] [FILE [PATCH]]

Parameters

-b, --backup
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.
--dry-run
Print the results of applying the patches without actually changing any files.
-f, --force
Assume that the user knows exactly what he or she is doing, and do not ask any questions. Skip patches whose headers do not say which file is to be patched; patch files even though they have the wrong version for the Prereq: line in the patch; and assume that patches are not reversed even if they look like they are. This option does not suppress commentary; use -s for that.
-i FILE, --input=FILE
Read the patch from FILE. If patchfile is -, read from standard input, the default.
-o FILE, --output=FILE
Send output to FILE 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.
-pNUMBER, --strip=NUMBER
Strip the smallest prefix containing NUMBER leading slashes from each file name found in the patch file. A sequence of one or more adjacent slashes is counted as a single slash. This controls how file names found in the patch file are treated, in case you keep your files in a different directory than the person who sent out the patch.
-R, --reverse
Assume that this patch was created with the old and new files swapped. patch attempts to swap each hunk around before applying it. Rejects come out in the swapped format.
-s, --silent, --quiet
Work silently, unless an error occurs.

Examples

Update the kernel to 2.6.30
cd '/usr/src/'
cp "${HOME}/tmp/patch-2.6.30.gz" '.'
zcat 'patch-2.6.30.gz' | patch -p0
Update the kernel to 2.6.30
cd '/usr/src/'
cp "${HOME}/tmp/patch-2.6.30.gz" '.'
gunzip 'patch-2.6.30.gz'
patch -p0 <'patch-2.6.30'
diff and patch
diff 'myscript.ori.sh' 'myscript.sh' >'myscript.patch'
patch 'myscript.ori.sh' --input='myscript.patch' --output='myscript.new.sh'

References

  1. man 1 'patch'