📄 filearchiv.html
字号:
>"command"</SPAN> in the <TTCLASS="REPLACEABLE"><I>whatis</I></TT> database. This is useful for identifying system commands and important configuration files. Consider it a simplified <BCLASS="COMMAND">man</B> command.</P><P><TTCLASS="USERINPUT"><B>$bash whatis whatis</B></TT><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="COMPUTEROUTPUT">whatis (1) - search the whatis database for complete words</TT></PRE></TD></TR></TABLE> </P><DIVCLASS="EXAMPLE"><HR><ANAME="WHAT"></A><P><B>Example 15-32. Exploring <TTCLASS="FILENAME">/usr/X11R6/bin</TT></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 3 # What are all those mysterious binaries in /usr/X11R6/bin? 4 5 DIRECTORY="/usr/X11R6/bin" 6 # Try also "/bin", "/usr/bin", "/usr/local/bin", etc. 7 8 for file in $DIRECTORY/* 9 do 10 whatis `basename $file` # Echoes info about the binary. 11 done 12 13 exit 0 14 15 # You may wish to redirect output of this script, like so: 16 # ./what.sh >>whatis.db 17 # or view it a page at a time on stdout, 18 # ./what.sh | less</PRE></TD></TR></TABLE><HR></DIV><P>See also <AHREF="loops.html#FILEINFO">Example 10-3</A>.</P></DD><DT><ANAME="VDIRREF"></A><BCLASS="COMMAND">vdir</B></DT><DD><P>Show a detailed directory listing. The effect is similar to <AHREF="external.html#LSREF">ls -lb</A>.</P><P>This is one of the GNU <ICLASS="FIRSTTERM">fileutils</I>.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>vdir</B></TT> <TTCLASS="COMPUTEROUTPUT">total 10 -rw-r--r-- 1 bozo bozo 4034 Jul 18 22:04 data1.xrolo -rw-r--r-- 1 bozo bozo 4602 May 25 13:58 data1.xrolo.bak -rw-r--r-- 1 bozo bozo 877 Dec 17 2000 employment.xrolo</TT> <TTCLASS="PROMPT">bash </TT><TTCLASS="USERINPUT"><B>ls -l</B></TT> <TTCLASS="COMPUTEROUTPUT">total 10 -rw-r--r-- 1 bozo bozo 4034 Jul 18 22:04 data1.xrolo -rw-r--r-- 1 bozo bozo 4602 May 25 13:58 data1.xrolo.bak -rw-r--r-- 1 bozo bozo 877 Dec 17 2000 employment.xrolo</TT> </PRE></TD></TR></TABLE> </P></DD><DT><ANAME="LOCATEREF"></A><BCLASS="COMMAND">locate</B>, <ANAME="SLOCATEREF"></A><BCLASS="COMMAND">slocate</B></DT><DD><P>The <BCLASS="COMMAND">locate</B> command searches for files using a database stored for just that purpose. The <BCLASS="COMMAND">slocate</B> command is the secure version of <BCLASS="COMMAND">locate</B> (which may be aliased to <BCLASS="COMMAND">slocate</B>).</P><P><TTCLASS="USERINPUT"><B>$bash locate hickson</B></TT><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="COMPUTEROUTPUT">/usr/lib/xephem/catalogs/hickson.edb</TT></PRE></TD></TR></TABLE></P></DD><DT><ANAME="READLINKREF"></A><BCLASS="COMMAND">readlink</B></DT><DD><P>Disclose the file that a symbolic link points to.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>readlink /usr/bin/awk</B></TT> <TTCLASS="COMPUTEROUTPUT">../../bin/gawk</TT> </PRE></TD></TR></TABLE> </P></DD><DT><ANAME="STRINGSREF"></A><BCLASS="COMMAND">strings</B></DT><DD><P>Use the <BCLASS="COMMAND">strings</B> command to find printable strings in a binary or data file. It will list sequences of printable characters found in the target file. This might be handy for a quick 'n dirty examination of a core dump or for looking at an unknown graphic image file (<TTCLASS="USERINPUT"><B>strings image-file | more</B></TT> might show something like <ICLASS="FIRSTTERM">JFIF</I>, which would identify the file as a <ICLASS="FIRSTTERM">jpeg</I> graphic). In a script, you would probably parse the output of <BCLASS="COMMAND">strings</B> with <AHREF="textproc.html#GREPREF">grep</A> or <AHREF="sedawk.html#SEDREF">sed</A>. See <AHREF="loops.html#BINGREP">Example 10-7</A> and <AHREF="loops.html#FINDSTRING">Example 10-9</A>.</P><DIVCLASS="EXAMPLE"><HR><ANAME="WSTRINGS"></A><P><B>Example 15-33. An <SPANCLASS="QUOTE">"improved"</SPAN> <ICLASS="FIRSTTERM">strings</I> command</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # wstrings.sh: "word-strings" (enhanced "strings" command) 3 # 4 # This script filters the output of "strings" by checking it 5 #+ against a standard word list file. 6 # This effectively eliminates gibberish and noise, 7 #+ and outputs only recognized words. 8 9 # =========================================================== 10 # Standard Check for Script Argument(s) 11 ARGS=1 12 E_BADARGS=65 13 E_NOFILE=66 14 15 if [ $# -ne $ARGS ] 16 then 17 echo "Usage: `basename $0` filename" 18 exit $E_BADARGS 19 fi 20 21 if [ ! -f "$1" ] # Check if file exists. 22 then 23 echo "File \"$1\" does not exist." 24 exit $E_NOFILE 25 fi 26 # =========================================================== 27 28 29 MINSTRLEN=3 # Minimum string length. 30 WORDFILE=/usr/share/dict/linux.words # Dictionary file. 31 # May specify a different 32 #+ word list file 33 #+ of one-word-per-line format. 34 35 36 wlist=`strings "$1" | tr A-Z a-z | tr '[:space:]' Z | \ 37 tr -cs '[:alpha:]' Z | tr -s '\173-\377' Z | tr Z ' '` 38 39 # Translate output of 'strings' command with multiple passes of 'tr'. 40 # "tr A-Z a-z" converts to lowercase. 41 # "tr '[:space:]'" converts whitespace characters to Z's. 42 # "tr -cs '[:alpha:]' Z" converts non-alphabetic characters to Z's, 43 #+ and squeezes multiple consecutive Z's. 44 # "tr -s '\173-\377' Z" converts all characters past 'z' to Z's 45 #+ and squeezes multiple consecutive Z's, 46 #+ which gets rid of all the weird characters that the previous 47 #+ translation failed to deal with. 48 # Finally, "tr Z ' '" converts all those Z's to whitespace, 49 #+ which will be seen as word separators in the loop below. 50 51 # **************************************************************** 52 # Note the technique of feeding the output of 'tr' back to itself, 53 #+ but with different arguments and/or options on each pass. 54 # **************************************************************** 55 56 57 for word in $wlist # Important: 58 # $wlist must not be quoted here. 59 # "$wlist" does not work. 60 # Why not? 61 do 62 63 strlen=${#word} # String length. 64 if [ "$strlen" -lt "$MINSTRLEN" ] # Skip over short strings. 65 then 66 continue 67 fi 68 69 grep -Fw $word "$WORDFILE" # Match whole words only. 70 # ^^^ # "Fixed strings" and 71 #+ "whole words" options. 72 73 done 74 75 76 exit $?</PRE></TD></TR></TABLE><HR></DIV></DD></DL></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="COMPARISONN1"></A>Comparison</B></P><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><ANAME="PATCHREF"></A></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><ANAME="ZDIFFREF"></A></P><P>Use <BCLASS="COMMAND">zdiff</B> to compare <ICLASS="FIRSTTERM">gzipped</I> files.</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><ANAME="DIFFSTATREF"></A></P><P>Use <BCLASS="COMMAND">diffstat</B> to create a histogram (point-distribution graph) of output from <BCLASS="COMMAND">diff</B>.</P></TD></TR></TABLE></DIV></DD><DT><ANAME="DIFF3REF"></A><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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -