📄 filearchiv.html
字号:
CLASS="COMMAND">compress</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">znew</B> command transforms <ICLASS="EMPHASIS">compressed</I> files into <ICLASS="EMPHASIS">gzipped</I> ones.</P></TD></TR></TABLE></DIV></DD><DT><BCLASS="COMMAND">sq</B></DT><DD><P>Yet another compression utility, a filter that works only on sorted ASCII word lists. It uses the standard invocation syntax for a filter, <BCLASS="COMMAND">sq < input-file > output-file</B>. Fast, but not nearly as efficient as <AHREF="filearchiv.html#GZIPREF">gzip</A>. The corresponding uncompression filter is <BCLASS="COMMAND">unsq</B>, invoked like <BCLASS="COMMAND">sq</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 output of <BCLASS="COMMAND">sq</B> may be piped to <BCLASS="COMMAND">gzip</B> for further compression.</P></TD></TR></TABLE></DIV></DD><DT><BCLASS="COMMAND">zip</B>, <BCLASS="COMMAND">unzip</B></DT><DD><P>Cross-platform file archiving and compression utility compatible with DOS <ICLASS="EMPHASIS">pkzip.exe</I>. <SPANCLASS="QUOTE">"Zipped"</SPAN> archives seem to be a more acceptable medium of exchange on the Internet than <SPANCLASS="QUOTE">"tarballs"</SPAN>.</P></DD><DT><BCLASS="COMMAND">unarc</B>, <BCLASS="COMMAND">unarj</B>, <BCLASS="COMMAND">unrar</B></DT><DD><P>These Linux utilities permit unpacking archives compressed with the DOS <ICLASS="EMPHASIS">arc.exe</I>, <ICLASS="EMPHASIS">arj.exe</I>, and <ICLASS="EMPHASIS">rar.exe</I> programs.</P></DD></DL></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="FAINFORMATION1"></A>File Information</B></P><DL><DT><ANAME="FILEREF"></A><BCLASS="COMMAND">file</B></DT><DD><P>A utility for identifying file types. The command <TTCLASS="USERINPUT"><B>file file-name</B></TT> will return a file specification for <TTCLASS="FILENAME">file-name</TT>, such as <TTCLASS="COMPUTEROUTPUT">ascii text</TT> or <TTCLASS="COMPUTEROUTPUT">data</TT>. It references the <AHREF="sha-bang.html#MAGNUMREF">magic numbers</A> found in <TTCLASS="FILENAME">/usr/share/magic</TT>, <TTCLASS="FILENAME">/etc/magic</TT>, or <TTCLASS="FILENAME">/usr/lib/magic</TT>, depending on the Linux/UNIX distribution.</P><P>The <TTCLASS="OPTION">-f</TT> option causes <BCLASS="COMMAND">file</B> to run in batch mode, to read from a designated file a list of filenames to analyze. The <TTCLASS="OPTION">-z</TT> option, when used on a compressed target file, forces an attempt to analyze the uncompressed file type.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>file test.tar.gz</B></TT> <TTCLASS="COMPUTEROUTPUT">test.tar.gz: gzip compressed data, deflated, last modified: Sun Sep 16 13:34:51 2001, os: Unix</TT> <TTCLASS="PROMPT">bash </TT><TTCLASS="USERINPUT"><B>file -z test.tar.gz</B></TT> <TTCLASS="COMPUTEROUTPUT">test.tar.gz: GNU tar archive (gzip compressed data, deflated, last modified: Sun Sep 16 13:34:51 2001, os: Unix)</TT> </PRE></TD></TR></TABLE> </P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 # Find sh and Bash scripts in a given directory: 2 3 DIRECTORY=/usr/local/bin 4 KEYWORD=Bourne 5 # Bourne and Bourne-Again shell scripts 6 7 file $DIRECTORY/* | fgrep $KEYWORD 8 9 # Output: 10 11 # /usr/local/bin/burn-cd: Bourne-Again shell script text executable 12 # /usr/local/bin/burnit: Bourne-Again shell script text executable 13 # /usr/local/bin/cassette.sh: Bourne shell script text executable 14 # /usr/local/bin/copy-cd: Bourne-Again shell script text executable 15 # . . .</PRE></TD></TR></TABLE> </P><DIVCLASS="EXAMPLE"><HR><ANAME="STRIPC"></A><P><B>Example 12-29. Stripping comments from C program files</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # strip-comment.sh: Strips out the comments (/* COMMENT */) in a C program. 3 4 E_NOARGS=0 5 E_ARGERROR=66 6 E_WRONG_FILE_TYPE=67 7 8 if [ $# -eq "$E_NOARGS" ] 9 then 10 echo "Usage: `basename $0` C-program-file" >&2 # Error message to stderr. 11 exit $E_ARGERROR 12 fi 13 14 # Test for correct file type. 15 type=`file $1 | awk '{ print $2, $3, $4, $5 }'` 16 # "file $1" echoes file type . . . 17 # Then awk removes the first field of this, the filename . . . 18 # Then the result is fed into the variable "type". 19 correct_type="ASCII C program text" 20 21 if [ "$type" != "$correct_type" ] 22 then 23 echo 24 echo "This script works on C program files only." 25 echo 26 exit $E_WRONG_FILE_TYPE 27 fi 28 29 30 # Rather cryptic sed script: 31 #-------- 32 sed ' 33 /^\/\*/d 34 /.*\*\//d 35 ' $1 36 #-------- 37 # Easy to understand if you take several hours to learn sed fundamentals. 38 39 40 # Need to add one more line to the sed script to deal with 41 #+ case where line of code has a comment following it on same line. 42 # This is left as a non-trivial exercise. 43 44 # Also, the above code deletes non-comment lines with a "*/" -- 45 #+ not a desirable result. 46 47 exit 0 48 49 50 # ---------------------------------------------------------------- 51 # Code below this line will not execute because of 'exit 0' above. 52 53 # Stephane Chazelas suggests the following alternative: 54 55 usage() { 56 echo "Usage: `basename $0` C-program-file" >&2 57 exit 1 58 } 59 60 WEIRD=`echo -n -e '\377'` # or WEIRD=$'\377' 61 [[ $# -eq 1 ]] || usage 62 case `file "$1"` in 63 *"C program text"*) sed -e "s%/\*%${WEIRD}%g;s%\*/%${WEIRD}%g" "$1" \ 64 | tr '\377\n' '\n\377' \ 65 | sed -ne 'p;n' \ 66 | tr -d '\n' | tr '\377' '\n';; 67 *) usage;; 68 esac 69 70 # This is still fooled by things like: 71 # printf("/*"); 72 # or 73 # /* /* buggy embedded comment */ 74 # 75 # To handle all special cases (comments in strings, comments in string 76 #+ where there is a \", \\" ...) the only way is to write a C parser 77 #+ (using lex or yacc perhaps?). 78 79 exit 0</PRE></TD></TR></TABLE><HR></DIV></DD><DT><ANAME="WHICHREF"></A><BCLASS="COMMAND">which</B></DT><DD><P><BCLASS="COMMAND">which command-xxx</B> gives the full path to <SPANCLASS="QUOTE">"command-xxx"</SPAN>. This is useful for finding out whether a particular command or utility is installed on the system.</P><P><TTCLASS="USERINPUT"><B>$bash which rm</B></TT><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="COMPUTEROUTPUT">/usr/bin/rm</TT></PRE></TD></TR></TABLE> </P></DD><DT><BCLASS="COMMAND">whereis</B></DT><DD><P>Similar to <BCLASS="COMMAND">which</B>, above, <BCLASS="COMMAND">whereis command-xxx</B> gives the full path to <SPANCLASS="QUOTE">"command-xxx"</SPAN>, but also to its <ICLASS="EMPHASIS">manpage</I>.</P><P><TTCLASS="USERINPUT"><B>$bash whereis rm</B></TT><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="COMPUTEROUTPUT">rm: /bin/rm /usr/share/man/man1/rm.1.bz2</TT></PRE></TD></TR></TABLE> </P></DD><DT><ANAME="WHATISREF"></A><BCLASS="COMMAND">whatis</B></DT><DD><P><BCLASS="COMMAND">whatis filexxx</B> looks up <SPANCLASS="QUOTE">"filexxx"</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 12-30. <BCLASS="COMMAND">Exploring <TTCLASS="FILENAME">/usr/X11R6/bin</TT></B></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><BCLASS="COMMAND">vdir</B></DT><DD><P>Show a detailed directory listing. The effect is similar to <AHREF="external.html#LSREF">ls -l</A>.</P><P>This is one of the GNU <ICLASS="EMPHASIS">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><BCLASS="COMMAND">locate</B>, <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><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><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 <TTCLASS="COMPUTEROUTPUT">JFIF</TT>, which would identify the file as a <ICLASS="EMPHASIS">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 12-31. An <SPANCLASS="QUOTE">"improved"</SPAN> <ICLASS="EMPHASIS">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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -