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

📄 ch19_05.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 19] 19.5 Using tar to Create and Unpack Archives </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:39:26Z"><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="ch19_01.htm"TITLE="19. Creating and Reading Archives"><LINKREL="prev"HREF="ch19_04.htm"TITLE="19.4 A Simple Version of unshar "><LINKREL="next"HREF="ch19_06.htm"TITLE="19.6 GNU tar Sampler "></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="ch19_04.htm"TITLE="19.4 A Simple Version of unshar "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 19.4 A Simple Version of unshar "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 19<BR>Creating and Reading Archives</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch19_06.htm"TITLE="19.6 GNU tar Sampler "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 19.6 GNU tar Sampler "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-3690">19.5 Using tar to Create and Unpack Archives </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-21342"></A><ACLASS="indexterm"NAME="AUTOID-21344"></A><ACLASS="indexterm"NAME="AUTOID-21346"></A>Many UNIX users think of<SPANCLASS="link"><EMCLASS="emphasis">tar</EM> (<ACLASS="linkend"HREF="ch20_01.htm#UPT-ART-8024"TITLE="tar in a Nutshell ">20.1</A>)</SPAN>as a utility for creating tapes.Like most UNIX utilities though, that's only the beginning. Forexample, you can use <EMCLASS="emphasis">tar</EM> for<SPANCLASS="link">copying directory trees (<ACLASS="linkend"HREF="ch18_16.htm"TITLE="Copying Directory Trees with (tar | tar) ">18.16</A>)</SPAN>.</P><PCLASS="para">One common use for <EMCLASS="emphasis">tar</EM> is creating archive files that can beshipped to other systems. We've already seen a utility for creating<SPANCLASS="link">shell archives (<ACLASS="linkend"HREF="ch19_02.htm"TITLE="Introduction to Shell Archives ">19.2</A>)</SPAN>,but there are a lot of things that a shell archive can't do.<EMCLASS="emphasis">tar</EM> is very useful when you're sendingbinary data; I've seen some <EMCLASS="emphasis">shar</EM> utilities that can handlebinary data, but they're rare, and I don't particularly like the waythey do it. If you use <EMCLASS="emphasis">tar</EM>, you can package severaldirectories into an archive, you can send directories that includelinks, you can preserve file ownership and access permissions, etc.</P><PCLASS="para">To create a <EMCLASS="emphasis">tar</EM> archive, use the <EMCLASS="emphasis">c</EM> (create) and <EMCLASS="emphasis">f</EM> (filename) options to save<EMCLASS="emphasis">tar</EM>'s output in a file:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cd /home/src/fsf</B></CODE>% <CODECLASS="userinput"><B>tar cf emacs.tar emacs</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">This command puts everything in the <EMCLASS="emphasis">emacs</EM> directory into afile (called a <EMCLASS="emphasis">tar file</EM>) named <EMCLASS="emphasis">emacs.tar</EM>. You can thengive this file to other users, via<SPANCLASS="link">FTP, UUCP (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>,or any other means.</P><PCLASS="para">Archives (no matter how you make them) are usually rather large, soit's common to <SPANCLASS="link">compress (<ACLASS="linkend"HREF="ch24_07.htm"TITLE="Compressing Files to Save Space ">24.7</A>)</SPAN>them, with a command like:<ACLASS="indexterm"NAME="AUTOID-21375"></A></P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>gzip emacs.tar</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">This creates the file <EMCLASS="emphasis">emacs.tar.gz</EM>, which should besignificantly smaller than the original <EMCLASS="emphasis">tar</EM> archive.</P><PCLASS="para">If you're going to use UUCP or FTP to transfer the file, this is goodenough; both UUCP and FTP know how to handle binary data. Oftenthough, you'd like to send the archive via<SPANCLASS="link">electronic mail (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>,and some mail programs only know how to handle<SPANCLASS="link">ASCII (<ACLASS="linkend"HREF="ch51_03.htm"TITLE="ASCII Characters: Listing and Getting Values ">51.3</A>)</SPAN>data.In that case, you'll need to createan ASCII version. To do this, use the<ACLASS="indexterm"NAME="AUTOID-21386"></A><SPANCLASS="link"><EMCLASS="emphasis">uuencode</EM> (<ACLASS="linkend"HREF="glossary.htm#UPT-ART-1010"TITLE="Glossary">52.9</A>)</SPAN>command.To read the file directly, repeat its name twice:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>uuencode emacs.tar.gz emacs.tar.gz &gt; emacs.tar.gz.uu</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">You can then insert <EMCLASS="emphasis">emacs.tar.gz.uu</EM> into a mail message andsend it to someone. Of course, the ASCII-only encoding won't be asefficient as the original binary file. It's about 33&nbsp;percent larger.[1]</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[1] If so, why bother <EMCLASS="emphasis">gzip</EM>ping? Why not forget about both<EMCLASS="emphasis">gzip</EM> and <EMCLASS="emphasis">uuencode</EM>? Well, you can't. Remember that<EMCLASS="emphasis">tar</EM> files are binary files to start with&nbsp;- even if every file inthe archive is an ASCII text file. You'd need to <EMCLASS="emphasis">uuencode</EM> afile before mailing it, anyway&nbsp;- so you'd still pay the 33 percent size penaltythat <EMCLASS="emphasis">uuencode</EM> incurs. Using <EMCLASS="emphasis">gzip</EM> minimizes the damage.</P></BLOCKQUOTE><PCLASS="para">If you'd rather, you can combine the steps above into one pipeline.Giving <EMCLASS="emphasis">tar</EM> the archive<SPANCLASS="link">filename (<ACLASS="linkend"HREF="ch13_13.htm"TITLE='The "Filename&quot; - '>13.13</A>)</SPAN>tells it to write toits standard output.That feeds the archive down the pipe:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>tar cf - emacs | gzip | uuencode emacs.tar.gz &gt; emacs.tar.gz.uu</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">What happens when you receive a uuencoded, compressed <EMCLASS="emphasis">tar</EM> file?The same thing, inreverse. You'll get a mail message that (after the various headerlines) looks something like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">begin 644 emacs.tar.gzM+DQ0&quot;D%L;&quot;!O9B!T:&amp;5S92!P&lt;F]B;&amp;5M&lt;R!C86X@8F4@&lt;V]L=F5D(&amp;)Y(&quot;)LM:6YK&lt;RPB(&amp;$@;65C:&amp;%N:7-M('=H:6-H&quot;F%L;&amp;]W&lt;R!A(&amp;9I;&amp;4@=&amp;\@:&amp;%VM92!T=V\@;W(@;6]R92!N86UE&lt;RX@(%5.25@@&lt;')O=FED97,@='=O(&amp;1I9F9EM&lt;F5N= IK:6YD&lt;R!O9B!L:6YK&lt;SH*+DQS($(*+DQI&quot;EQF0DAA&lt;F0@;&amp;EN:W-&lt;</PRE></BLOCKQUOTE></P><PCLASS="para">So you save the message in a file, complete with headers. Let's sayyou call this file <EMCLASS="emphasis">mailstuff</EM>. How do you get the original filesback? Use the following sequence of commands:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>uudecode mailstuff</B></CODE>% <CODECLASS="userinput"><B>gunzip emacs.tar.gz</B></CODE>% <CODECLASS="userinput"><B>tar xf emacs.tar</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-21422"></A><ACLASS="indexterm"NAME="AUTOID-21424"></A>The <EMCLASS="emphasis">uudecode</EM> command creates the file <EMCLASS="emphasis">emacs.tar.gz</EM>.Then <EMCLASS="emphasis">gunzip</EM> recreates your original <EMCLASS="emphasis">tar</EM> file, and<EMCLASS="emphasis">tar&nbsp;xf</EM> extracts the individual files from the archive.Article<ACLASS="xref"HREF="ch19_07.htm"TITLE="Extracting Files from a Compressed Archive ">19.7</A>shows a more efficient method&nbsp;- and also explains the <EMCLASS="emphasis">tar o</EM> option,which many System V users will need.</P><PCLASS="para">By the way, <EMCLASS="emphasis">tar</EM> is so flexible precisely because of UNIX'sfile-oriented design: everything, even a tape drive, &quot;looks like&quot; afile. So <EMCLASS="emphasis">tar</EM> creates a certain kind of file and sends it outinto the world; it usually lands on a tape, but you can put itsomewhere else if you want.   With most operating systems, a tape utility would know how to talk toa tape drive, and that's all.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">ML</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="ch19_04.htm"TITLE="19.4 A Simple Version of unshar "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 19.4 A Simple Version of unshar "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="ch19_06.htm"TITLE="19.6 GNU tar Sampler "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 19.6 GNU tar Sampler "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">19.4 A Simple Version of unshar </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">19.6 GNU tar Sampler </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 + -