PROLOGThis 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.
NAMEdiff --- compare two files
diff [-c|-e|-f|-u|-C n|-U n] [-br] file1 file2
DESCRIPTIONThe 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.
OPTIONSThe 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:
- 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.
- 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).
- Produce output in a form suitable as input for the ed utility, which can then be used to convert file1 into file2.
- 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.
recursively to files and directories of the same name when
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.
- 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).
OPERANDSThe 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.
STDINThe standard input shall be used only if one of the file1 or file2 operands references standard input. See the INPUT FILES section.
INPUT FILESThe input files may be of any type.
ENVIRONMENT VARIABLESThe following environment variables shall affect the execution of diff:
- 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.)
- If set to a non-empty string value, override the values of all the other internationalization variables.
- 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).
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.
- Determine the locale for affecting the format of file timestamps written with the -C and -c options.
- Determine the location of message catalogs for the processing of LC_MESSAGES.
- 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.
Diff Directory Comparison FormatIf 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 FormatIn 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 FormatThe 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>
Diff -e Output FormatWith 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 FormatWith the -f option, an alternative format of script shall be produced. It is similar to that produced by -e, with the following differences:
- 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.
- The command form <lines> <command-letter> used by -e is reversed. For example, 10c with -e would be c10 with -f.
- The form used for ranges of line numbers is <space>-separated, rather than <comma>-separated.
Diff -c or -C Output FormatWith 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"
"*** %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 FormatThe -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>
STDERRThe standard error shall be used only for diagnostic messages.
EXIT STATUSThe following exit values shall be returned:
- No differences were found.
- Differences were found.
- An error occurred.
CONSEQUENCES OF ERRORSDefault.
The following sections are informative.
APPLICATION USAGEIf 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.
EXAMPLESIf 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
RATIONALEThe -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>
SEE ALSOcmp, comm, ed, find The Base Definitions volume of POSIX.1-2008, Chapter 8, Environment Variables, Section 12.2, Utility Syntax Guidelines
COPYRIGHTPortions 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 .