ArticleslgStudy

computer science

Diff

Diff 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 Diff rather than just read about it. In short: diff is a shell command that compares the content of files and reports differences. The term diff is also used to identify the output of the command and is used as a verb for running the command.

Key takeaways

  • Diff 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 Diff to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Diff from memory before moving on to harder problems.

Reference excerpt

diff is a shell command that compares the content of files and reports differences. The term diff is also used to identify the output of the command and is used as a verb for running the command. To diff files, one runs diff to create a diff. Typically, the command is used to compare text files, but it does support comparing binary files. If one of the input files contains non-textual data, the command defaults to brief-mode, reporting only a summary indication of whether the files differ. With the --text option, it always reports line-based differences; however, the output may be difficult to understand, as binary data is generally not structured in lines like text. Although the command is primarily used ad hoc to analyze changes between two files, a special use is for creating a patch file for use with the patch command – which was specifically designed to use a diff output report as a patch file. POSIX standardized the diff and patch commands including their shared file format.

History

The original diff utility was developed in the early 1970s for the Unix operating system, at Bell Labs in Murray Hill, New Jersey. It was part of the 5th Edition of Unix released in 1974, and was written by Douglas McIlroy, and James Hunt. This research was published in a 1976 paper co-written with James W. Hunt, who developed an initial prototype of diff. The algorithm this paper described became known as the Hunt–Szymanski algorithm. McIlroy's work was preceded and influenced by Steve Johnson's comparison program on GECOS and Mike Lesk's proof program. Proof also originated on Unix and, like diff, produced line-by-line changes and even used angle-brackets (">" and "<") to indicate line insertions and deletions in the program's output. The heuristics used in these early applications were, however, deemed unreliable. The potential usefulness of a diff tool led McIlroy to research and design a more robust tool for a variety of tasks that could perform well under the processing and size limitations of the PDP-11's hardware. His approach to the problem resulted from collaboration with individuals at Bell Labs, including Alfred Aho, Elliot Pinson, Jeffrey Ullman, and Harold S. Stone. In the context of Unix, the use of the ed line editor provided diff with the natural ability to create machine-usable "edit scripts". These edit scripts, when saved to a file, can, along with the original file, be reconstituted by ed into the modified file in its entirety. This greatly reduced the secondary storage necessary to maintain multiple versions of a file. McIlroy considered writing a post-processor for diff where a variety of output formats could be designed and implemented; however, he found it more frugal and simpler to have diff be responsible for generating the syntax and reverse-order input accepted by the ed command. In 1984, Larry Wall created the patch utility (releasing its source code on the mod.sources and net.sources newsgroups) for patching text files, using the output from diff plus the diff input file with the content before changes to create a file with the content after changes. X/Open Portability Guide issue 2 of 1987 includes diff. Context mode was added in POSIX.1-2001 (issue 6). Unified mode was added in POSIX.1-2008 (issue 7). In diff's early years, common uses included comparing changes in software source code and markup for technical documents, verifying program debugging output, comparing filesystem listings, and analyzing computer assembly code. The output targeted for ed was motivated to provide compression for a sequence of modifications made to a file. The Source Code Control System (SCCS) and its ability to archive revisions emerged in the late 1970s as a consequence of storing edit scripts from diff.

Algorithm Unlike edit distance notions used for other purposes, diff is line-oriented rather than character-oriented, but it is like Levenshtein distance in that it tries to determine the smallest set of deletions and insertions to create one file from the other. The operation of diff is based on solving the longest common subsequence problem. In this problem, given two sequences of items:

a b c d f g h j q z

a b c d e f g i j k r x y z

and we want to find the longest sequence of items that is present in both original sequences in the same order. That is, we want to find a new sequence that can be obtained from the first original sequence by deleting some items and from the second original sequence by deleting other items. We also want this sequence to be as long as possible. In this case, it is

a b c d f g j z

From a longest common subsequence, it is only a small step to get diff-like output: if an item is absent in the subsequence but present in the first original sequence, it must have been deleted (as indicated by the '-' marks, below). If it is absent in the subsequence but present in the second original sequence, it must have been inserted (as indicated by the '+' marks).

e h i q k r x y + - + - + + + +

Use The diff command accepts two arguments like: diff original new. Commonly, the arguments each identify normal files; however, if the two arguments identify directories, then the command compares corresponding files in the directories. With the -r option, it recursively descends matching subdirectories to compare files with corresponding relative paths.

Default output format The example below shows the original and new file content as well as the resulting diff output in the default format. The output is shown with coloring to improve readability. GNU diff can colorize output when the --color option is used; when no --color option is specified, color output is disabled by default.

In this default format, a indicates added, d indicates deleted, and c indicates changed. The line number of the original file appears before the single-letter code, and the line number of the new file appears after. The less-than and greater-than signs (at the beginning of lines that are added, deleted, or changed) indicate which file the lines appear in. Addition lines are added to the original file and appear in the new file. Deletion lines are deleted from the original file and are missing in the new file. By default, lines common to both files are not shown. Lines that have moved are shown as added at their new location and as deleted from their old location. However, some diff tools highlight moved lines.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Diff

Start with the simplest possible case. Write down what Diff 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 Diff 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 Diff 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 Diff

In research
Diff 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 Diff 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
Diff is common in secondary-school and first-year university syllabi. It links to neighbouring topics 1974 software, Data differencing, Formal languages, so understanding it makes those chapters shorter.
In everyday life
Look for Diff 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 “Diff” →

How to study Diff in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Diff 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 Diff out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Diff in simple terms?

diff is a shell command that compares the content of files and reports differences. The term diff is also used to identify the output of the command and is used as a verb for running the command.

Why does Diff 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 Diff?

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 Diff.

Tags

  • 1974 software
  • Data differencing
  • Formal languages
  • Free file comparison tools
  • Inferno (operating system) commands
  • Pattern matching
  • Plan 9 commands
  • Standard Unix programs
  • Unix SUS2008 utilities

Keep exploring