ArticleslgStudy

computer science

Patch (Unix)

Patch (Unix) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Patch (Unix) rather than just read about it. In short: 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.

Patch (Unix) — main illustration
Patch (Unix) — illustration

Key takeaways

  • Patch (Unix) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Patch (Unix) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Patch (Unix) from memory before moving on to harder problems.

Reference excerpt

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 for applying 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 they 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 (which later became comp.sources.unix) in May 1985. patch was added to XPG4, which later became POSIX. Wall's code remains the basis of "patch" programs provided in OpenBSD, FreeBSD, and schilytools. The Open Software Foundation, which merged into The Open Group, is said to have maintained a derived version. The GNU project/FSF maintains its patch, forked from the Larry Wall version. The repository is different from that of GNU diffutils, but the documentation is managed together. Originally written for Unix, the command has also been ported to Windows (via GnuWin32 and UnxUtils) and many other platforms. An implementation is part of ASCII's MSX-DOS2 Tools for MSX-DOS version 2.

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, patchFile, that encodes how to generate newFile from oldFile. The -u option selects to output in unified diff format.

The following command applies the changes to the file identified in patchFile.

Patching a file in a subdirectory requires the additional -pnumber 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:

If the file used as the original is significantly different from the actual original file, the patch cannot be cleanly applied. For example, if lines of text are inserted at the beginning, the line numbers referred to in the patch will be incorrect. patch 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.

See also Quilt (software) rsync xdelta List of POSIX commands IBM Mainframe utility IEBUPDTE, a mainframe patch program, created about 20 years earlier (circa ~1964).

References

External links

GNU Diffutils (includes diff and patch); Documentation GNU tools for Win32 – Win32 port of tools, including diff and patch Dickey, Thomas E. "DIFFSTAT – make histogram from diff-output". Archived from the original on 2020-01-22. Retrieved 2020-05-01

Illustrations

Patch (Unix) illustration

Worked examples

Example 1 — a first encounter with Patch (Unix)

Start with the simplest possible case. Write down what Patch (Unix) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Patch (Unix) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Patch (Unix) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Patch (Unix)

In research
Patch (Unix) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Patch (Unix) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Patch (Unix) is common in secondary-school and first-year university syllabi. It links to neighbouring topics 1984 software, Patch utilities, Plan 9 commands, so understanding it makes those chapters shorter.
In everyday life
Look for Patch (Unix) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Patch (Unix)” →

How to study Patch (Unix) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Patch (Unix) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Patch (Unix) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Patch (Unix) in simple terms?

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.

Why does Patch (Unix) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Patch (Unix)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Patch (Unix).

Tags

  • 1984 software
  • Patch utilities
  • Plan 9 commands
  • Unix SUS2008 utilities

Keep exploring