diff — Compare two text files and show the differences
Purpose
The diff command attempts to determine the minimal set of changes needed to convert a file whose name is specified by the path1 argument into the file specified by the path2 argument.
If either (but only one) file name is –, diff uses a copy of the standard input for that file. If exactly one of path1 or path2 is a directory, diff uses a file in that directory with the same name as the other file name. If both are directories, diff compares files with the same file names under the two directories; however, it does not compare files in subdirectories unless you specify the –r option. When comparing two directories, diff does not compare character special files, or FIFO special files with any other files.
By default, output consists of descriptions of the changes in a style like that of the ed
text editor. A line indicating the type of change is given. The three types are a
(append), d (delete), and c (change). The output is symmetric: A
delete in path1 is the counterpart of an append in
path2. diff prefixes each operation with a line number (or range)
in path1 and suffixes each with a line number (or range) in
path2. After the line giving the type of change, diff displays the
deleted or added lines, prefixing lines from path1 with
< and lines from path2 with >.
Options
- –b
- Ignores trailing blanks and tabs and considers adjacent groups of blanks and tabs elsewhere in input lines to be equivalent.
- –C n
- Is equivalent to -cn.
- –c[n]
- Shows n lines of context before and after each change. The default
value for n is 3. diff marks lines removed from
path1 with –, lines added to path2 with
+, and lines changed in both files with
!. - –Difname
- Displays output that is the appropriate input to the C preprocessor to generate the contents of path2 when ifname is defined, and the contents of path1 when ifname is not defined.
- –e
- writes out a script of commands for the ed text editor, which converts path1 to path2. diff sends the output to the standard output.
- –f
- Writes a script similar to the one produced under –e to standard output, but does not adjust the line numbers to reflect earlier editing changes; instead, they correspond to the line numbers in path1.
- –H
- Uses the half-hearted (–h) algorithm only if the normal algorithm runs out of system resources.
- –h
- Uses a fast, half-hearted algorithm instead of the normal diff algorithm. This algorithm can handle arbitrarily large files; however, it is not particularly good at finding a minimal set of differences in files with many differences.
- –i
- Ignores the case of letters when doing the comparison.
- –m
- Produces the contents of path2 with extra formatter request lines
interspersed to show which lines were added (those with vertical bars in the right margin) and
deleted (indicated by a
*in the right margin). - –n
- Is accepted for compatibility, but performs no function.
- –r
- Compares corresponding files under the directories, and recursively compares corresponding files under corresponding subdirectories under the directories. You can use this option when you specify two directory names on the command line.
- –s
- Compares two directories, file by file, and prints messages for identical files between the two directories.
- –w
- Ignores white space when making the comparison.
Examples
The following example illustrates the effect of the –c option on the output of the diff command. The following two files, price1 and price2, are compared with and without the use of the –c option.
Company X Price List:
$ 0.39 -- Package of Groat Clusters
$ 5.00 -- Candy Apple Sampler Pack
$ 12.00 -- Box of Crunchy Frog Chocolates
$ 15.99 -- Instant Rain (Just Add Water)
$ 20.00 -- Asparagus Firmness Meter
$ 25.00 -- Package of Seeds for 35 Herbs
$ 30.00 -- Child's Riding Hood (Red)
$ 35.00 -- Genuine Placebos
$ 45.00 -- Case of Simulated Soy Bean Oil
$ 75.88 -- No-Name Contact Lenses
$ 99.99 -- Kiddie Destructo-Bot
$125.00 -- Emperor's New ClothesCompany X Price List:
$ 0.39 -- Package of Groat Clusters
$ 5.49 -- Candy Apple Sampler Pack
$ 12.00 -- Box of Crunchy Frog Chocolates
$ 15.99 -- Instant Rain (Just Add Water)
$ 17.00 -- Simulated Naugahyde cleaner
$ 20.00 -- Asparagus Firmness Meter
$ 25.00 -- Package of Seeds for 35 Herbs
$ 30.00 -- Child's Riding Hood (Red)
$ 35.00 -- Genuine Placebos
$ 45.00 -- Case of Simulated Soy Bean Oil
$ 75.88 -- No-Name Contact Lenses
$ 99.99 -- Kiddie Destructo-Bot diff price1 price2 results in the following output:
4c4
< $ 5.00 -- Candy Apple Sampler Pack
--->
$ 5.49 -- Candy Apple Sampler Pack
6a7
> $ 17.00 -- Simulated Naugahyde cleaner
14d14
< $125.00 -- Emperor's New Clothes
The addition of the –c option, as in: diff -c price1 price2 results in
the following output:
*** price1 Wed Mar 04 10:08:40 1993
--- price2 Wed Mar 04 10:09:10 1993
***************
*** 1,9 ****
Company X Price List:
$ 0.39 -- Package of Groat Clusters
! $ 5.00 -- Candy Apple Sampler Pack
$ 12.00 -- Box of Crunchy Frog Chocolates
$ 15.99 -- Instant Rain (Just Add Water)
$ 20.00 -- Asparagus Firmness Meter
$ 25.00 -- Package of Seeds for 35 Herbs
$ 30.00 -- Child's Riding Hood (Red)
--- 1,10 ----
Company X Price List:
$ 0.39 -- Package of Groat Clusters
! $ 5.49 -- Candy Apple Sampler Pack
$ 12.00 -- Box of Crunchy Frog Chocolates
$ 15.99 -- Instant Rain (Just Add Water)
+ $ 17.00 -- Simulated Naugahyde cleaner
$ 20.00 -- Asparagus Firmness Meter
$ 25.00 -- Package of Seeds for 35 Herbs
$ 30.00 -- Child's Riding Hood (Red)
***************
*** 11,14 ****
$ 45.00 -- Case of Simulated Soy Bean Oil
$ 75.88 -- No-Name Contact Lenses
$ 99.99 -- Kiddie Destructo-Bot
- $125.00 -- Emperor's New Clothes
--- 12,14 ----diff
–c marks lines removed from price1 with –, lines added to price1 with
+ and lines changed in both files with !. In the example, diff shows
the default three lines of context around each changed line. One line was changed in both files
(marked with !), one line was added to price1 (marked with +), and
one line was removed from price1 (marked with –).
Localization
- LANG
- LC_ALL
- LC_CTYPE
- LC_MESSAGES
- LC_TIME
Exit Values
0- No differences between the files compared.
1- diff compared the files and found them to be different.
2- Failure due to any of the following:
- Incorrect command-line argument
- Inability to find one of the input files
- Out of memory
- Read error on one of the input files
4- At least one of the files is a binary file containing embedded NUL (\0) bytes or newlines that are more than LINE_MAX bytes apart.
Messages and Return Codes
- Binary files filename and filename differ
- The two specified files are binary files. diff has compared the two files and found that they are not identical. With binary files, diff does not try to report the differences.
- file filename: no such file or directory
- The specified filename does not exist. filename was either typed explicitly, or generated by diff from the directory of one file argument and the basename of the other.
- Files file1 and file2 are identical
- The –s option was specified and the two named files are identical.
- Common subdirectories: name and name
- This message appears when diff is comparing the contents of directories, but you have not specified –r. When diff discovers two subdirectories with the same name, it reports that the directories exist, but it does not try to compare the contents of the two directories.
- Insufficient memory (try diff –h)
- diff ran out of memory for generating the data structures used in the file differencing algorithm (see Limits). The –h option of diff can handle any size file without running out of memory.
- Internal error—cannot create temporary file
- diff was unable to create a working file that it needed. Ensure that you either have a directory /tmp or that the environment contains a variable TMPDIR, which names a directory where diff can store temporary files. Also, ensure that there is sufficient file space in this directory.
- Missing ifdef symbol after -D
- You did not specify a conditional label on the command line after the –D option.
- Only one file may be –
- Of the two input files normally found on the command line of diff, only one can be the standard input.
- Too many lines in filename
- A file of more than the maximum number of lines (see Limits) was given to diff.
Limits
The longest input line is 1024 bytes. Except under –h, files are limited to INT_MAX lines. INT_MAX is defined in limits.h.
Portability
POSIX.2, X/Open Portability Guide, UNIX systems.
The –D, –f, –H, –h, –i, –m, –s, and –w options, and the n argument to the –c option, are extensions of the POSIX standard.
Related Commands
J. W. Hunt and M. D. McIlroy, An Algorithm for Differential File
Comparison
, Computing Science Technical Report 41 (Bell Telephone Laboratories).
cmp, comm