From Surf Wiki (app.surf) — the open knowledge base
Patch (Unix)
Command for applying changes to text files
Command for applying changes to text files
| Field | Value |
|---|---|
| name | patch |
| screenshot | TuxOnIce patching.png |
| caption | A screenshot of using patch |
| author | Larry Wall |
| developer | Paul Eggert, Wayne Davison, David MacKenzie, Andreas Grünbacher |
| released | |
| operating system | Unix and Unix-like, Plan 9, MSX-DOS, Microsoft Windows |
| platform | Cross-platform |
| genre | Command |
**patch** is a shell command that updates text files according to instructions in a separate file, called a patch file. The patch file is a text file that lists the differences between the input file and the desired content. The command is designed to support patch files created via diff. A user creates a patch file by running diff for two versions of a file (original and target) which produces a list of differences that patch can later use to generate the target file from the original file plus the patch file. The term patch is also a verb meaning to apply a patch.
Developed by a programmer for other programmers, patch was frequently used for updating source code to a newer version. Because of this, many people came to associate patches with source code, whereas patches can in fact be applied to any text. Patched files do not accumulate any unneeded text, which is what some people perceive based on the English meaning of the word; patch is as capable of removing text as it is of adding it.
Patches described here should not be confused with binary patches, which, although can be conceptually similar, are distributed to update binary files comprising the program to a new release.
The original patch program was written by Larry Wall (who went on to create the Perl programming language) and posted to mod.sources{{cite newsgroup
|author-link=Larry_Wall
|access-date=2024-07-14
|url-status=live
|archive-url=https://web.archive.org/web/20230404223018/https://groups.google.com/g/mod.sources/c/xSQM63e39YY/m/apNNJSkJi0gJ
|archive-date=2023-04-04
Use
As patch files are text, they can be used with processes such as quality review and modification via a text editor.
In addition to the diff command, a patch file can be produced by other programs, such as Subversion, CVS, RCS, Mercurial and Git.
Patches have been the crucial component of many source control systems, including CVS.
Advanced diffs
When more advanced diffs are used, patches can be applied even to files that have been modified in the meantime, as long as those modifications do not interfere with the patch. This is achieved by using "context diffs" and "unified diffs" (also known as "unidiffs"), which surround each change with context, which is the text immediately before and after the changed part. Patch can then use this context to locate the region to be patched even if it has been displaced by changes earlier in the file, using the line numbers in the diffs as a starting point. Because of this property, context and unified diffs are the preferred form of patches for submission to many software projects.
The above features make diff and patch especially popular for exchanging modifications to open-source software. Outsiders can download the latest publicly available source code, make modifications to it, and send them, in diff form, to the development team. Using diffs, the development team has the ability to effectively review the patches before applying them, and can apply them to a newer code base than the one the outside developer had access to.
Examples
The following command creates a patch file, , that encodes how to generate from . The option selects to output in unified diff format.
$ diff -u oldFile newFile > patchFile
The following command applies the changes to the file identified in patchFile.
$ patch < patchFile
Patching a file in a subdirectory requires the additional -p*number* option, where number is 1 if the base directory of the source tree is included in the diff, and 0 otherwise.
A patch can be undone, or reversed, with the -R option:
$ patch -R < patchFile
If the file used as the original is significantly different from the actual original file, the patch cannot be cleanly be applied. For example, if lines of text are inserted at the beginning, the line numbers referred to in the patch will be incorrect. is able to recover from this, by looking at nearby lines to relocate the text to be patched. It will also recover when lines of context (for context and unified diffs) are altered; this is described as fuzz.
References
References
- {{man. cu. patch. SUS
- http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/usr.bin/patch/ {{Webarchive. link. (2017-06-07 OpenBSD patch source)
- {{man. 1. FreeBSD
- "Schilytools".
- "Patch.c\SRC - patch.git - GNU patch".
- [https://archive.org/details/MSXDOS2TOOLS MSX-DOS2 Tools User's Manual by ASCII Corporation]
This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page.
Ask Mako anything about Patch (Unix) — get instant answers, deeper analysis, and related topics.
Research with MakoFree with your Surf account
Create a free account to save articles, ask Mako questions, and organize your research.
Sign up freeThis content may have been generated or modified by AI. CloudSurf Software LLC is not responsible for the accuracy, completeness, or reliability of AI-generated content. Always verify important information from primary sources.
Report