diff (1)
PROLOG
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.NAME
diff --- compare two filesSYNOPSIS
diff [-c|-e|-f|-u|-C n|-U n] [-br] file1 file2
DESCRIPTION
The diff utility shall compare the contents of file1 and file2 and write to standard output a list of changes necessary to convert file1 into file2. This list should be minimal. No output shall be produced if the files are identical.OPTIONS
The diff utility shall conform to the Base Definitions volume of POSIX.1-2008, Section 12.2, Utility Syntax Guidelines. The following options shall be supported:- -b
- Cause any amount of white space at the end of a line to be treated as a single <newline> (that is, the white-space characters preceding the <newline> are ignored) and other strings of white-space characters, not including <newline> characters, to compare equal.
- -c
- Produce output in a form that provides three lines of copied context.
- -C n
- Produce output in a form that provides n lines of copied context (where n shall be interpreted as a positive decimal integer).
- -e
- Produce output in a form suitable as input for the ed utility, which can then be used to convert file1 into file2.
- -f
- Produce output in an alternative form, similar in format to -e, but not intended to be suitable as input for the ed utility, and in the opposite order.
- -r
-
Apply
diff
recursively to files and directories of the same name when
file1
and
file2
are both directories.
-
The diff utility shall detect infinite loops; that is, entering a previously visited directory that is an ancestor of the last file encountered. When it detects an infinite loop, diff shall write a diagnostic message to standard error and shall either recover its position in the hierarchy or terminate.
-
- -u
- Produce output in a form that provides three lines of unified context.
- -U n
- Produce output in a form that provides n lines of unified context (where n shall be interpreted as a non-negative decimal integer).
OPERANDS
The following operands shall be supported:- file1, file2
- A pathname of a file to be compared. If either the file1 or file2 operand is '-', the standard input shall be used in its place. If both file1 and file2 are directories, diff shall not compare block special files, character special files, or FIFO special files to any files and shall not compare regular files to directories. Further details are as specified in Diff Directory Comparison Format. The behavior of diff on other file types is implementation-defined when found in directories. If only one of file1 and file2 is a directory, diff shall be applied to the non-directory file and the file contained in the directory file with a filename that is the same as the last component of the non-directory file.
STDIN
The standard input shall be used only if one of the file1 or file2 operands references standard input. See the INPUT FILES section.INPUT FILES
The input files may be of any type.ENVIRONMENT VARIABLES
The following environment variables shall affect the execution of diff:- LANG
- Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of POSIX.1-2008, Section 8.2, Internationalization Variables for the precedence of internationalization variables used to determine the values of locale categories.)
- LC_ALL
- If set to a non-empty string value, override the values of all the other internationalization variables.
- LC_CTYPE
- Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multi-byte characters in arguments and input files).
- LC_MESSAGES
-
Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error and informative messages written to standard output. - LC_TIME
- Determine the locale for affecting the format of file timestamps written with the -C and -c options.
- NLSPATH
- Determine the location of message catalogs for the processing of LC_MESSAGES.
- TZ
- Determine the timezone used for calculating file timestamps written with a context format. If TZ is unset or null, an unspecified default timezone shall be used.
ASYNCHRONOUS EVENTS
Default.STDOUT
Diff Directory Comparison Format
If both file1 and file2 are directories, the following output formats shall be used. In the POSIX locale, each file that is present in only one directory shall be reported using the following format:
-
"Only in %s: %s\n", <directory pathname>, <filename>
-
"Common subdirectories: %s and %s\n", <directory1 pathname>, <directory2 pathname>
-
"diff %s %s %s\n", <diff_options>, <filename1>, <filename2>
Diff Binary Output Format
In the POSIX locale, if one or both of the files being compared are not text files, it is implementation-defined whether diff uses the binary file output format or the other formats as specified below. The binary file output format shall contain the pathnames of two files being compared and the string dqdifferdq. If both files being compared are text files, depending on the options specified, one of the following formats shall be used to write the differences.Diff Default Output Format
The default (without -e, -f, -c, -C, -u, or -U options) diff utility output shall contain lines of these forms:
-
"%da%d\n", <num1>, <num2> "%da%d,%d\n", <num1>, <num2>, <num3> "%dd%d\n", <num1>, <num2> "%d,%dd%d\n", <num1>, <num2>, <num3> "%dc%d\n", <num1>, <num2> "%d,%dc%d\n", <num1>, <num2>, <num3> "%dc%d,%d\n", <num1>, <num2>, <num3> "%d,%dc%d,%d\n", <num1>, <num2>, <num3>, <num4>
-
"< %s", <line>
-
"> %s", <line>
-
"---\n"
Diff -e Output Format
With the -e option, a script shall be produced that shall, when provided as input to ed, along with an appended w (write) command, convert file1 into file2. Only the a (append), c (change), d (delete), i (insert), and s (substitute) commands of ed shall be used in this script. Text lines, except those consisting of the single character <period> ('.'), shall be output as they appear in the file.Diff -f Output Format
With the -f option, an alternative format of script shall be produced. It is similar to that produced by -e, with the following differences:- 1.
- It is expressed in reverse sequence; the output of -e orders changes from the end of the file to the beginning; the -f from beginning to end.
- 2.
- The command form <lines> <command-letter> used by -e is reversed. For example, 10c with -e would be c10 with -f.
- 3.
- The form used for ranges of line numbers is <space>-separated, rather than <comma>-separated.
Diff -c or -C Output Format
With the -c or -C option, the output format shall consist of affected lines along with surrounding lines of context. The affected lines shall show which ones need to be deleted or changed in file1, and those added from file2. With the -c option, three lines of context, if available, shall be written before and after the affected lines. With the -C option, the user can specify how many lines of context are written. The exact format follows. The name and last modification time of each file shall be output in the following format:
-
"*** %s %s\n", file1, <file1 timestamp> "--- %s %s\n", file2, <file2 timestamp>
-
date "+%a %b %e %T %Y"
-
"***************\n"
-
"*** %d,%d ****\n", <beginning line number>, <ending line number>
-
"*** %d ****\n", <ending line number>
-
" %s", <unaffected_line>
-
"- %s", <deleted_line>
-
"! %s", <changed_line>
-
"--- %d,%d ----\n", <beginning line number>, <ending line number>
-
"--- %d ----\n", <ending line number>
-
"+ %s", <added_line>
Diff -u or -U Output Format
The -u or -U options behave like the -c or -C options, except that the context lines are not repeated; instead, the context, deleted, and added lines are shown together, interleaved. The exact format follows. The name and last modification time of each file shall be output in the following format:
-
"--- %s %s%s %s, file1, <file1 timestamp>, <file1 frac>, <file1 zone> "+++ %s %s%s %s, file2, <file2 timestamp>, <file2 frac>, <file2 zone>
-
date '+%Y-%m-%d %H:%M:%S'
-
"@@ -%s +%s @@", <file1 range>, <file2 range>
-
"%1d", <beginning line number>
-
"%1d,%1d", <beginning line number>, <number of lines>
-
" %s", <unaffected_line>
-
"-%s", <deleted_line>
-
"+%s", <added_line>
STDERR
The standard error shall be used only for diagnostic messages.OUTPUT FILES
None.EXTENDED DESCRIPTION
None.EXIT STATUS
The following exit values shall be returned:- 0
- No differences were found.
- 1
- Differences were found.
- >1
- An error occurred.
CONSEQUENCES OF ERRORS
Default.The following sections are informative.
APPLICATION USAGE
If lines at the end of a file are changed and other lines are added, diff output may show this as a delete and add, as a change, or as a change and add; diff is not expected to know which happened and users should not care about the difference in output as long as it clearly shows the differences between the files.EXAMPLES
If dir1 is a directory containing a directory named x, dir2 is a directory containing a directory named x, dir1/x and dir2/x both contain files named date.out, and dir2/x contains a file named y, the command:
-
diff -r dir1 dir2
-
Common subdirectories: dir1/x and dir2/x Only in dir2/x: y diff -r dir1/x/date.out dir2/x/date.out 1c1 < Mon Jul 2 13:12:16 PDT 1990 --- > Tue Jun 19 21:41:39 PDT 1990
RATIONALE
The -h option was omitted because it was insufficiently specified and does not add to applications portability. Historical implementations employ algorithms that do not always produce a minimum list of differences; the current language about making every effort is the best this volume of POSIX.1-2008 can do, as there is no metric that could be employed to judge the quality of implementations against any and all file contents. The statement ``This list should be minimal'' clearly implies that implementations are not expected to provide the following output when comparing two 100-line files that differ in only one character on a single line:
-
1,100c1,100 all 100 lines from file1 preceded with "< " --- all 100 lines from file2 preceded with "> "
-
"diff %s %s %s\n", <diff_options>, <filename1>, <filename2>
FUTURE DIRECTIONS
None.SEE ALSO
cmp, comm, ed, find The Base Definitions volume of POSIX.1-2008, Chapter 8, Environment Variables, Section 12.2, Utility Syntax GuidelinesCOPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, Copyright (C) 2013 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. (This is POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at www.unix.org/online.html .Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see www.kernel.org/doc/man-pages/reporting_bugs.html .