⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch24_07.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 24] 24.7 Compressing Files to Save Space </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly &amp; Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:41:32Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch24_01.htm"TITLE="24. Other Ways to Get Disk Space"><LINKREL="prev"HREF="ch24_06.htm"TITLE="24.6 Save Space with Tab Characters "><LINKREL="next"HREF="ch24_08.htm"TITLE="24.8 Save Space: tar and compress a Directory Tree "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch24_06.htm"TITLE="24.6 Save Space with Tab Characters "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 24.6 Save Space with Tab Characters "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 24<BR>Other Ways to Get Disk Space</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch24_08.htm"TITLE="24.8 Save Space: tar and compress a Directory Tree "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 24.8 Save Space: tar and compress a Directory Tree "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-6130">24.7 Compressing Files to Save Space </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-26426"></A><ACLASS="indexterm"NAME="AUTOID-26428"></A>Most files can be &quot;squeezed&quot; to take up less space.Let's say you have a text file. Each letter occupies a byte, butalmost all of the characters in the file are alphanumeric orpunctuation, and there are only about 70 such characters.Furthermore, most of the characters are (usually) lowercase;furthermore, the letter &quot;e&quot; turns up more often than &quot;z,&quot; the letter&quot;e&quot; often shows up in pairs, and so on.All in all, you don't really need a full eight-bit byte per character. Ifyou're clever, you can reduce the amount of space a file occupies by50 percent or more.</P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="gzip">gzip</A><BR></TH><TDVALIGN="TOP"><ACLASS="indexterm"NAME="AUTOID-26433"></A>Compression algorithms are a complex topic that we can't discuss here.Fortunately, you don't need to know anything about them. Many UNIXsystems have a good compression utility built in. It's called<EMCLASS="emphasis">compress</EM>.Unfortunately, though, the <EMCLASS="emphasis">compress</EM> algorithm seems to becovered by software patents; many users avoid it for that reason.A newer utility that's even better, and doesn't have patent problems,is GNU's <EMCLASS="emphasis">gzip</EM>.<ACLASS="indexterm"NAME="AUTOID-26438"></A>Those of you who don't have <EMCLASS="emphasis">gzip</EM> can find it on theCD-ROM.</TD></TR></TABLE><PCLASS="para">To compress a file, just give thecommand:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>gzip</B></CODE> <CODECLASS="replaceable"><I>filename</I></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">The file's name is changed to <CODECLASS="replaceable"><I>filename</I></CODE><CODECLASS="literal">.gz</CODE>.The <EMCLASS="emphasis">-v</EM> option asks <EMCLASS="emphasis">gzip</EM> totell you how much space you saved. The savings are usuallybetween 40 and 90 percent.</P><PCLASS="para">If the file <EMCLASS="emphasis">shouldn't</EM> be compressed&nbsp;- that is, if the file has<SPANCLASS="link">hard links (<ACLASS="linkend"HREF="ch18_04.htm"TITLE="More About Links ">18.4</A>)</SPAN>or the corresponding file already exists-<EMCLASS="emphasis">gzip</EM> prints a message.You can use the <EMCLASS="emphasis">-f</EM> option to &quot;force&quot; <EMCLASS="emphasis">gzip</EM> to compress such a file.This might be better if you're using <EMCLASS="emphasis">gzip</EM> within a shellscript and don't want to worry about files that might <EMCLASS="emphasis">not</EM> becompressed.</P><PCLASS="para">Compressed files are always binary files; even if they started out astext files, you can't read them. To get back the original file, usethe <EMCLASS="emphasis">gunzip</EM> utility:<ACLASS="indexterm"NAME="AUTOID-26462"></A></P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>gunzip</B></CODE> <CODECLASS="replaceable"><I>filename</I></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">(<EMCLASS="emphasis">gunzip</EM> also handles files from <EMCLASS="emphasis">compress</EM>,or you can use <EMCLASS="emphasis">uncompress</EM> if you'd rather.)You can omit the <EMCLASS="emphasis">.gz</EM> at the end of the filename. If youjust want to read the file but don't want to restore the original <ACLASS="indexterm"NAME="AUTOID-26473"></A>version, use the command <EMCLASS="emphasis">gzcat</EM>; this justdecodes the file and dumps it to standard output.It's particularly convenient to pipe <EMCLASS="emphasis">gzcat</EM> into<SPANCLASS="link"><EMCLASS="emphasis">more</EM> (<ACLASS="linkend"HREF="ch25_03.htm"TITLE="Using more to Page Through Files ">25.3</A>)</SPAN>or<SPANCLASS="link"><EMCLASS="emphasis">grep</EM> (<ACLASS="linkend"HREF="ch27_01.htm#UPT-ART-7420"TITLE="Different Versions of grep ">27.1</A>)</SPAN>.(There's a <EMCLASS="emphasis">zcat</EM> for <EMCLASS="emphasis">compress</EM>ed files, but <EMCLASS="emphasis">gzcat</EM>can handle those files too.)</P><PCLASS="para">The CD-ROM has several scripts that work on compressed files,uncompressing and recompressing them automatically:editing with<SPANCLASS="link"><EMCLASS="emphasis">zvi</EM>, <EMCLASS="emphasis">zex</EM>, and <EMCLASS="emphasis">zed</EM> (<ACLASS="linkend"HREF="ch24_11.htm"TITLE="Edit Compressed Files with zvi, zex, and zed ">24.11</A>)</SPAN>;viewing with<SPANCLASS="link"><EMCLASS="emphasis">zmore</EM>, <EMCLASS="emphasis">zless</EM>, and <EMCLASS="emphasis">zpg</EM> (<ACLASS="linkend"HREF="ch25_05.htm"TITLE="Page Through Compressed, RCS, Unprintable Files ">25.5</A>)</SPAN>;or running almost any command that can read from a pipe with<SPANCLASS="link"><EMCLASS="emphasis">zloop</EM> (<ACLASS="linkend"HREF="ch24_10.htm"TITLE="zloop: Run a Command on Compressed Files ">24.10</A>)</SPAN>.</P><PCLASS="para">There are a number of other compression utilities floating around theUNIX world.<EMCLASS="emphasis">gzip</EM> also works on other operating systems, though.It's reliable and freely available.So <EMCLASS="emphasis">gzip</EM> has become the utility that more people choose.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">ML</SPAN>, <SPANCLASS="authorinitials">JP</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch24_06.htm"TITLE="24.6 Save Space with Tab Characters "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 24.6 Save Space with Tab Characters "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch24_08.htm"TITLE="24.8 Save Space: tar and compress a Directory Tree "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 24.8 Save Space: tar and compress a Directory Tree "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">24.6 Save Space with Tab Characters </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">24.8 Save Space: tar and compress a Directory Tree </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed &amp; awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -