📄 diff.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy, see www.w3.org"><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 --><!-- Copyright (c) 2001-2004 IEEE and The Open Group, All Rights Reserved --><title>diff</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="diff"></a> <a name="tag_04_37"></a><!-- diff --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2004 Edition<br>Copyright © 2001-2004 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_04_37_01"></a>NAME</h4><blockquote>diff - compare two files</blockquote><h4><a name="tag_04_37_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><p><code><tt>diff</tt> <b>[</b><tt>-c| -e| -f| -C</tt> <i>n</i><b>][</b><tt>-br</tt><b>]</b> <i>file1 file2</i></code></p></blockquote><h4><a name="tag_04_37_03"></a>DESCRIPTION</h4><blockquote><p>The <i>diff</i> utility shall compare the contents of <i>file1</i> and <i>file2</i> and write to standard output a list ofchanges necessary to convert <i>file1</i> into <i>file2</i>. This list should be minimal. No output shall be produced if the filesare identical.</p></blockquote><h4><a name="tag_04_37_04"></a>OPTIONS</h4><blockquote><p>The <i>diff</i> utility shall conform to the Base Definitions volume of IEEE Std 1003.1-2001, <a href="../basedefs/xbd_chap12.html#tag_12_02">Section 12.2, Utility Syntax Guidelines</a>.</p><p>The following options shall be supported:</p><dl compact><dt><b>-b</b></dt><dd>Cause any amount of white space at the end of a line to be treated as a single <newline> (that is, the white-spacecharacters preceding the <newline> are ignored) and other strings of white-space characters, not including <newline>s,to compare equal.</dd><dt><b>-c</b></dt><dd>Produce output in a form that provides three lines of context.</dd><dt><b>-C n</b></dt><dd>Produce output in a form that provides <i>n</i> lines of context (where <i>n</i> shall be interpreted as a positive decimalinteger).</dd><dt><b>-e</b></dt><dd>Produce output in a form suitable as input for the <a href="../utilities/ed.html"><i>ed</i></a> utility, which can then be usedto convert <i>file1</i> into <i>file2</i>.</dd><dt><b>-f</b></dt><dd>Produce output in an alternative form, similar in format to <b>-e</b>, but not intended to be suitable as input for the <ahref="../utilities/ed.html"><i>ed</i></a> utility, and in the opposite order.</dd><dt><b>-r</b></dt><dd>Apply <i>diff</i> recursively to files and directories of the same name when <i>file1</i> and <i>file2</i> are bothdirectories.</dd></dl></blockquote><h4><a name="tag_04_37_05"></a>OPERANDS</h4><blockquote><p>The following operands shall be supported:</p><dl compact><dt><i>file1</i>, <i>file2</i></dt><dd>A pathname of a file to be compared. If either the <i>file1</i> or <i>file2</i> operand is <tt>'-'</tt>, the standard inputshall be used in its place.</dd></dl><p>If both <i>file1</i> and <i>file2</i> are directories, <i>diff</i> shall not compare block special files, character specialfiles, or FIFO special files to any files and shall not compare regular files to directories. Further details are as specified in<a href="#tag_04_37_10_01">Diff Directory Comparison Format</a>. The behavior of <i>diff</i> on other file types isimplementation-defined when found in directories.</p><p>If only one of <i>file1</i> and <i>file2</i> is a directory, <i>diff</i> shall be applied to the non-directory file and the filecontained in the directory file with a filename that is the same as the last component of the non-directory file.</p></blockquote><h4><a name="tag_04_37_06"></a>STDIN</h4><blockquote><p>The standard input shall be used only if one of the <i>file1</i> or <i>file2</i> operands references standard input. See theINPUT FILES section.</p></blockquote><h4><a name="tag_04_37_07"></a>INPUT FILES</h4><blockquote><p>The input files may be of any type.</p></blockquote><h4><a name="tag_04_37_08"></a>ENVIRONMENT VARIABLES</h4><blockquote><p>The following environment variables shall affect the execution of <i>diff</i>:</p><dl compact><dt><i>LANG</i></dt><dd>Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume ofIEEE Std 1003.1-2001, <a href="../basedefs/xbd_chap08.html#tag_08_02">Section 8.2, Internationalization Variables</a> forthe precedence of internationalization variables used to determine the values of locale categories.)</dd><dt><i>LC_ALL</i></dt><dd>If set to a non-empty string value, override the values of all the other internationalization variables.</dd><dt><i>LC_CTYPE</i></dt><dd>Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte asopposed to multi-byte characters in arguments and input files).</dd><dt><i>LC_MESSAGES</i></dt><dd>Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error andinformative messages written to standard output.</dd><dt><i>LC_TIME</i></dt><dd>Determine the locale for affecting the format of file timestamps written with the <b>-C</b> and <b>-c</b> options.</dd><dt><i>NLSPATH</i></dt><dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">Determine the location of message catalogs for the processing of <i>LC_MESSAGES .</i> <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt><i>TZ</i></dt><dd>Determine the timezone used for calculating file timestamps written with the <b>-C</b> and <b>-c</b> options. If <i>TZ</i> isunset or null, an unspecified default timezone shall be used.</dd></dl></blockquote><h4><a name="tag_04_37_09"></a>ASYNCHRONOUS EVENTS</h4><blockquote><p>Default.</p></blockquote><h4><a name="tag_04_37_10"></a>STDOUT</h4><blockquote><h5><a name="tag_04_37_10_01"></a>Diff Directory Comparison Format</h5><p>If both <i>file1</i> and <i>file2</i> are directories, the following output formats shall be used.</p><p>In the POSIX locale, each file that is present in only one directory shall be reported using the following format:</p><pre><tt>"Only in %s: %s\n", <</tt><i>directory pathname</i><tt>>, <</tt><i>filename</i><tt>></tt></pre><p>In the POSIX locale, subdirectories that are common to the two directories may be reported with the following format:</p><pre><tt>"Common subdirectories: %s and %s\n", <</tt><i>directory1 pathname</i><tt>>, <</tt><i>directory2 pathname</i><tt>></tt></pre><p>For each file common to the two directories if the two files are not to be compared, the following format shall be used in thePOSIX locale:</p><pre><tt>"File %s is a %s while file %s is a %s\n", <</tt><i>directory1 pathname</i><tt>>, <</tt><i>file type of directory1 pathname</i><tt>>, <</tt><i>directory2 pathname</i><tt>>, <</tt><i>file type of directory2 pathname</i><tt>></tt></pre><p>For each file common to the two directories, if the files are compared and are identical, no output shall be written. If the twofiles differ, the following format is written:</p><pre><tt>"diff %s %s %s\n", <</tt><i>diff_options</i><tt>>, <</tt><i>filename1</i><tt>>, <</tt><i>filename2</i><tt>></tt></pre><p>where <<i>diff_options</i>> are the options as specified on the command line.</p><p>All directory pathnames listed in this section shall be relative to the original command line arguments. All other names offiles listed in this section shall be filenames (pathname components).</p><h5><a name="tag_04_37_10_02"></a>Diff Binary Output Format</h5><p>In the POSIX locale, if one or both of the files being compared are not text files, an unspecified format shall be used thatcontains the pathnames of two files being compared and the string <tt>"differ"</tt>.</p><p>If both files being compared are text files, depending on the options specified, one of the following formats shall be used towrite the differences.</p><h5><a name="tag_04_37_10_03"></a>Diff Default Output Format</h5><p>The default (without <b>-e</b>, <b>-f</b>, <b>-c</b>, or <b>-C</b> options) <i>diff</i> utility output shall contain lines ofthese forms:</p><pre><tt>"%da%d\n", <</tt><i>num1</i><tt>>, <</tt><i>num2</i><tt>><br>"%da%d,%d\n", <</tt><i>num1</i><tt>>, <</tt><i>num2</i><tt>>, <</tt><i>num3</i><tt>><br>"%dd%d\n", <</tt><i>num1</i><tt>>, <</tt><i>num2</i><tt>><br>"%d,%dd%d\n", <</tt><i>num1</i><tt>>, <</tt><i>num2</i><tt>>, <</tt><i>num3</i><tt>><br>"%dc%d\n", <</tt><i>num1</i><tt>>, <</tt><i>num2</i><tt>><br>"%d,%dc%d\n", <</tt><i>num1</i><tt>>, <</tt><i>num2</i><tt>>, <</tt><i>num3</i><tt>><br>"%dc%d,%d\n", <</tt><i>num1</i><tt>>, <</tt><i>num2</i><tt>>, <</tt><i>num3</i><tt>><br>"%d,%dc%d,%d\n", <</tt><i>num1</i><tt>>, <</tt><i>num2</i><tt>>, <</tt><i>num3</i><tt>>, <</tt><i>num4</i><tt>></tt></pre><p>These lines resemble <a href="../utilities/ed.html"><i>ed</i></a> subcommands to convert <i>file1</i> into <i>file2</i>. Theline numbers before the action letters shall pertain to <i>file1</i>; those after shall pertain to <i>file2</i>. Thus, byexchanging <i>a</i> for <i>d</i> and reading the line in reverse order, one can also determine how to convert <i>file2</i> into<i>file1</i>. As in <a href="../utilities/ed.html"><i>ed</i></a>, identical pairs (where <i>num1</i>= <i>num2</i>) are abbreviatedas a single number.</p><p>Following each of these lines, <i>diff</i> shall write to standard output all lines affected in the first file using theformat:</p><pre><tt>"<<img src="../images/delta.gif" border="0">%s", <</tt><i>line</i><tt>></tt></pre><p>and all lines affected in the second file using the format:</p><pre><tt>"><img src="../images/delta.gif" border="0">%s", <</tt><i>line</i><tt>></tt></pre><p>If there are lines affected in both <i>file1</i> and <i>file2</i> (as with the <b>c</b> subcommand), the changes are separatedwith a line consisting of three hyphens:</p><pre><tt>"---\n"</tt></pre><h5><a name="tag_04_37_10_04"></a>Diff -e Output Format</h5><p>With the <b>-e</b> option, a script shall be produced that shall, when provided as input to <a href="../utilities/ed.html"><i>ed</i></a>, along with an appended <b>w</b> (write) command, convert <i>file1</i> into <i>file2</i>. Onlythe <b>a</b> (append), <b>c</b> (change), <b>d</b> (delete), <b>i</b> (insert), and <b>s</b> (substitute) commands of <a href="../utilities/ed.html"><i>ed</i></a> shall be used in this script. Text lines, except those consisting of the single characterperiod ( <tt>'.'</tt> ), shall be output as they appear in the file.</p><h5><a name="tag_04_37_10_05"></a>Diff -f Output Format</h5><p>With the <b>-f</b> option, an alternative format of script shall be produced. It is similar to that produced by <b>-e</b>, withthe following differences:</p><ol><li><p>It is expressed in reverse sequence; the output of <b>-e</b> orders changes from the end of the file to the beginning; the<b>-f</b> from beginning to end.</p></li><li><p>The command form <<i>lines</i>> <<i>command-letter</i>> used by <b>-e</b> is reversed. For example, 10<i>c</i> with<b>-e</b> would be <i>c</i>10 with <b>-f</b>.</p></li><li><p>The form used for ranges of line numbers is <space>-separated, rather than comma-separated.</p></li></ol><h5><a name="tag_04_37_10_06"></a>Diff -c or -C Output Format</h5><p>With the <b>-c</b> or <b>-C</b> option, the output format shall consist of affected lines along with surrounding lines ofcontext. The affected lines shall show which ones need to be deleted or changed in <i>file1</i>, and those added from <i>file2</i>.With the <b>-c</b> option, three lines of context, if available, shall be written before and after the affected lines. With the<b>-C</b> option, the user can specify how many lines of context are written. The exact format follows.</p><p>The name and last modification time of each file shall be output in the following format:</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -