📄 filearchiv.html
字号:
><DL><DT><ANAME="DIFFREF"></A><BCLASS="COMMAND">diff</B>, <BCLASS="COMMAND">patch</B></DT><DD><P><BCLASS="COMMAND">diff</B>: flexible file comparison utility. It compares the target files line-by-line sequentially. In some applications, such as comparing word dictionaries, it may be helpful to filter the files through <AHREF="textproc.html#SORTREF">sort</A> and <BCLASS="COMMAND">uniq</B> before piping them to <BCLASS="COMMAND">diff</B>. <TTCLASS="USERINPUT"><B>diff file-1 file-2</B></TT> outputs the lines in the files that differ, with carets showing which file each particular line belongs to.</P><P>The <TTCLASS="OPTION">--side-by-side</TT> option to <BCLASS="COMMAND">diff</B> outputs each compared file, line by line, in separate columns, with non-matching lines marked. The <TTCLASS="OPTION">-c</TT> and <TTCLASS="OPTION">-u</TT> options likewise make the output of the command easier to interpret.</P><P>There are available various fancy frontends for <BCLASS="COMMAND">diff</B>, such as <BCLASS="COMMAND">sdiff</B>, <BCLASS="COMMAND">wdiff</B>, <BCLASS="COMMAND">xdiff</B>, and <BCLASS="COMMAND">mgdiff</B>. </P><DIVCLASS="TIP"><TABLECLASS="TIP"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/tip.png"HSPACE="5"ALT="Tip"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The <BCLASS="COMMAND">diff</B> command returns an exit status of 0 if the compared files are identical, and 1 if they differ. This permits use of <BCLASS="COMMAND">diff</B> in a test construct within a shell script (see below).</P></TD></TR></TABLE></DIV><P>A common use for <BCLASS="COMMAND">diff</B> is generating difference files to be used with <BCLASS="COMMAND">patch</B> The <TTCLASS="OPTION">-e</TT> option outputs files suitable for <BCLASS="COMMAND">ed</B> or <BCLASS="COMMAND">ex</B> scripts.</P><P><BCLASS="COMMAND">patch</B>: flexible versioning utility. Given a difference file generated by <BCLASS="COMMAND">diff</B>, <BCLASS="COMMAND">patch</B> can upgrade a previous version of a package to a newer version. It is much more convenient to distribute a relatively small <SPANCLASS="QUOTE">"diff"</SPAN> file than the entire body of a newly revised package. Kernel <SPANCLASS="QUOTE">"patches"</SPAN> have become the preferred method of distributing the frequent releases of the Linux kernel.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 patch -p1 <patch-file 2 # Takes all the changes listed in 'patch-file' 3 # and applies them to the files referenced therein. 4 # This upgrades to a newer version of the package.</PRE></TD></TR></TABLE></P><P>Patching the kernel:</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 cd /usr/src 2 gzip -cd patchXX.gz | patch -p0 3 # Upgrading kernel source using 'patch'. 4 # From the Linux kernel docs "README", 5 # by anonymous author (Alan Cox?).</PRE></TD></TR></TABLE></P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The <BCLASS="COMMAND">diff</B> command can also recursively compare directories (for the filenames present).</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>diff -r ~/notes1 ~/notes2</B></TT> <TTCLASS="COMPUTEROUTPUT">Only in /home/bozo/notes1: file02 Only in /home/bozo/notes1: file03 Only in /home/bozo/notes2: file04</TT> </PRE></TD></TR></TABLE> </P></TD></TR></TABLE></DIV><DIVCLASS="TIP"><TABLECLASS="TIP"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/tip.png"HSPACE="5"ALT="Tip"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Use <BCLASS="COMMAND">zdiff</B> to compare <ICLASS="EMPHASIS">gzipped</I> files.</P></TD></TR></TABLE></DIV></DD><DT><BCLASS="COMMAND">diff3</B></DT><DD><P>An extended version of <BCLASS="COMMAND">diff</B> that compares three files at a time. This command returns an exit value of 0 upon successful execution, but unfortunately this gives no information about the results of the comparison.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>diff3 file-1 file-2 file-3</B></TT> <TTCLASS="COMPUTEROUTPUT">==== 1:1c This is line 1 of "file-1". 2:1c This is line 1 of "file-2". 3:1c This is line 1 of "file-3"</TT> </PRE></TD></TR></TABLE> </P></DD><DT><BCLASS="COMMAND">sdiff</B></DT><DD><P>Compare and/or edit two files in order to merge them into an output file. Because of its interactive nature, this command would find little use in a script.</P></DD><DT><BCLASS="COMMAND">cmp</B></DT><DD><P>The <BCLASS="COMMAND">cmp</B> command is a simpler version of <BCLASS="COMMAND">diff</B>, above. Whereas <BCLASS="COMMAND">diff</B> reports the differences between two files, <BCLASS="COMMAND">cmp</B> merely shows at what point they differ.</P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Like <BCLASS="COMMAND">diff</B>, <BCLASS="COMMAND">cmp</B> returns an exit status of 0 if the compared files are identical, and 1 if they differ. This permits use in a test construct within a shell script.</P></TD></TR></TABLE></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="FILECOMP"></A><P><B>Example 12-32. Using <BCLASS="COMMAND">cmp</B> to compare two files within a script.</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 3 ARGS=2 # Two args to script expected. 4 E_BADARGS=65 5 E_UNREADABLE=66 6 7 if [ $# -ne "$ARGS" ] 8 then 9 echo "Usage: `basename $0` file1 file2" 10 exit $E_BADARGS 11 fi 12 13 if [[ ! -r "$1" || ! -r "$2" ]] 14 then 15 echo "Both files to be compared must exist and be readable." 16 exit $E_UNREADABLE 17 fi 18 19 cmp $1 $2 &> /dev/null # /dev/null buries the output of the "cmp" command. 20 # cmp -s $1 $2 has same result ("-s" silent flag to "cmp") 21 # Thank you Anders Gustavsson for pointing this out. 22 # 23 # Also works with 'diff', i.e., diff $1 $2 &> /dev/null 24 25 if [ $? -eq 0 ] # Test exit status of "cmp" command. 26 then 27 echo "File \"$1\" is identical to file \"$2\"." 28 else 29 echo "File \"$1\" differs from file \"$2\"." 30 fi 31 32 exit 0</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="TIP"><TABLECLASS="TIP"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/tip.png"HSPACE="5"ALT="Tip"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Use <BCLASS="COMMAND">zcmp</B> on <ICLASS="EMPHASIS">gzipped</I> files.</P></TD></TR></TABLE></DIV></DD><DT><BCLASS="COMMAND">comm</B></DT><DD><P>Versatile file comparison utility. The files must be sorted for this to be useful.</P><P><BCLASS="COMMAND">comm <TTCLASS="REPLACEABLE"><I>-options</I></TT> <TTCLASS="REPLACEABLE"><I>first-file</I></TT> <TTCLASS="REPLACEABLE"><I>second-file</I></TT></B></P><P><TTCLASS="USERINPUT"><B>comm file-1 file-2</B></TT> outputs three columns: <UL><LI><P>column 1 = lines unique to <TTCLASS="FILENAME">file-1</TT></P></LI><LI><P>column 2 = lines unique to <TTCLASS="FILENAME">file-2</TT></P></LI><LI><P>column 3 = lines common to both.</P></LI></UL></P><P>The options allow suppressing output of one or more columns. <UL><LI><P><TTCLASS="OPTION">-1</TT> suppresses column <TTCLASS="LITERAL">1</TT></P></LI><LI><P><TTCLASS="OPTION">-2</TT> suppresses column <TTCLASS="LITERAL">2</TT></P></LI><LI><P><TTCLASS="OPTION">-3</TT> suppresses column <TTCLASS="LITERAL">3</TT></P></LI><LI><P><TTCLASS="OPTION">-12</TT> suppresses both columns <TTCLASS="LITERAL">1</TT> and <TTCLASS="LITERAL">2</TT>, etc.</P></LI></UL> </P></DD></DL></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="FAUTILS1"></A>Utilities</B></P><DL><DT><BCLASS="COMMAND">basename</B></DT><DD><P>Strips the path information from a file name, printing only the file name. The construction <TTCLASS="USERINPUT"><B>basename $0</B></TT> lets the script know its name, that is, the name it was invoked by. This can be used for <SPANCLASS="QUOTE">"usage"</SPAN> messages if, for example a script is called with missing arguments: <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 echo "Usage: `basename $0` arg1 arg2 ... argn"</PRE></TD></TR></TABLE> </P></DD><DT><BCLASS="COMMAND">dirname</B></DT><DD><P>Strips the <BCLASS="COMMAND">basename</B> from a filename, printing only the path information.</P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P><BCLASS="COMMAND">basename</B> and <BCLASS="COMMAND">dirname</B> can operate on any arbitrary string. The argument does not need to refer to an existing file, or even be a filename for that matter (see <AHREF="contributed-scripts.html#DAYSBETWEEN">Example A-7</A>).</P></TD></TR></TABLE></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="EX35"></A><P><B>Example 12-33. <BCLASS="COMMAND">basename</B> and <BCLASS="COMMAND">dirname</B></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 3 a=/home/bozo/daily-journal.txt 4 5 echo "Basename of /home/bozo/daily-journal.txt = `basename $a`" 6 echo "Dirname of /home/bozo/daily-journal.txt = `dirname $a`" 7 echo 8 echo "My own home is `basename ~/`." # `basename ~` also works. 9 echo "The home of my home is `dirname ~/`." # `dirname ~` also works. 10 11 exit 0</PRE></TD></TR></TABLE><HR></DIV></DD><DT><BCLASS="COMMAND">split</B>, <BCLASS="COMMAND">csplit</B></DT><DD><P>These are utilities for splitting a file into smaller chunks. They are usually used for splitting up large files in order to back them up on floppies or preparatory to e-mailing or uploading them.</P><P>The <BCLASS="COMMAND">csplit</B> command splits a file according to <ICLASS="EMPHASIS">context</I>, the split occuring where patterns are matched.</P></DD><DT><BCLASS="COMMAND">sum</B>, <BCLASS="COMMAND">cksum</B>, <ANAME="MD5SUMREF"></A><BCLASS="COMMAND">md5sum</B>, <BCLASS="COMMAND">sha1sum</B></DT><DD><P>These are utilities for generating checksums. A <ICLASS="EMPHASIS">checksum</I> is a number mathematically calculated from the contents of a file, for the purpose of checking its integrity. A script might refer to a list of checksums for security purposes, such as ensuring that the contents of key system files have not been altered or corrupted. For security applications, use the <BCLASS="COMMAND">md5sum</B> (<BCLASS="COMMAND">m</B>essage <BCLASS="COMMAND">d</B>igest <BCLASS="COMMAND">5</B> check<BCLASS="COMMAND">sum</B>) command, or better yet, the newer <BCLASS="COMMAND">sha1sum</B> (Secure Hash Algorithm).</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>cksum /boot/vmlinuz</B></TT> <TTCLASS="COMPUTEROUTPUT">1670054224 804083 /boot/vmlinuz</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo -n "Top Secret" | cksum</B></TT> <TTCLASS="COMPUTEROUTPUT">3391003827 10</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>md5sum /boot/vmlinuz</B></TT> <TTCLASS="COMPUTEROUTPUT">0f43eccea8f09e0a0b2b5cf1dcf333ba /boot/vmlinuz</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo -n "Top Secret" | md5sum</B></TT> <TTCLASS="COMPUTEROUTPUT">8babc97a6f62a4649716f4df8d61728f -</TT> </PRE></TD></TR></TABLE> </P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The <BCLASS="COMMAND">cksum</B
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -