Lably
Skip to content

Diff

Shell command for comparing file content

  • 16 min read
  • 4 chapters

Related on Lably

Through Command (computing), Text file and Binary file

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.

Sign in to keep reading

3 more chapters are free with a Lably account.

More in Computing and communication

Source and licence. Adapted from the Wikipedia article “Diff” (this version, 2026-09-16) by its contributors, shared under CC BY-SA 4.0. Lablypedia has left out its tables, references and most pictures, and linked it to other Lablypedia articles.