📄 0078-0081.html
字号:
<HTML>
<HEAD>
<TITLE>Sams Teach Yourself Linux in 24 Hours:Manipulation and Searching Commands:EarthWeb Inc.-</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!-- ISBN=0672311623 //-->
<!-- TITLE=Sams Teach Yourself Linux in 24 Hours//-->
<!-- AUTHOR=Bill Ball//-->
<!-- PUBLISHER=Macmillan Computer Publishing//-->
<!-- IMPRINT=Sams//-->
<!-- CHAPTER=05 //-->
<!-- PAGES=0063-0082 //-->
<!-- UNASSIGNED1 //-->
<!-- UNASSIGNED2 //-->
<P><CENTER>
<a href="0075-0077.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0082-0082.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-78"><P>Page 78</P></A>
<P>This section introduces you to the basics of archiving and compressing files. However,
for details concerning using these programs for the purpose of system management or
backing up your system, see Hour 23, "Archiving." Read on, and you'll learn how to build your
own archives and save disk space.
</P>
<H4><A NAME="ch05_ 17">
Creating Archives with the Tape Archive Command
</A></H4>
<P>The tar (tape archive) program has its roots in the early days of computing before
floppy drives, hard disks, and CD-ROMs. Software was distributed and backed up on large
reels of magnetic tape, so one of the first programs to run
on computers was a tape reader. Over time, the tar program has proved its merit as a convenient way to transport files, and
many programs you'll find for Linux come packaged in
tar archives (your Red Hat CD-ROM uses the rpm program to package files; see Hour 22, "Red Hat Tools").
</P>
<P>Using tar, you can create an archive file containing multiple directories and multiple
files. The version of tar installed on your system also supports a
-z option to use the gzip program to compress your archive
(gzip is discussed later in this chapter).
</P>
<P>The tar command has a bewildering array of options, but it's not hard to use. You can
quickly and easily create an archive of any desired directory.
</P>
<P>First, you'll create a directory with three files, and then create a subdirectory with
another three files:
</P>
<!-- CODE //-->
<PRE>
# mkdir mydir
# cd mydir
# touch file1 file2 file3
# mkdir mydir2
# cd mydir2
# touch file21 file22 file23
# cd ../..
# tree mydir
mydir
|-- file1
|-- file2
|-- file3
`-- mydir2
|-- file21
|-- file22
`-- file23
1 directory, 6 files
</PRE>
<!-- END CODE //-->
<P>Now that you have built a directory, create a
tar archive with this command:
</P>
<!-- CODE //-->
<PRE>
# tar -c mydir > mydir.tar
# ls -l
total 11
drwxrwxr-x 3 bball bball 1024 Nov 14 16:48 mydir
-rw-rw-r-- 1 bball bball 10240 Nov 14 16:58 mydir.tar
</PRE>
<!-- END CODE //-->
<A NAME="PAGENUM-79"><P>Page 79</P></A>
<P>Notice that your original directory is left untouched. By default,
tar will not delete the original directories or files. You can
use the --remove-files if you'd like to do this, but it's
not recommended. If you'd like to see what's going on, you can use the
-v option, like this:
</P>
<!-- CODE //-->
<PRE>
# tar -cv mydir > mydir.tar
mydir/
mydir/file1
mydir/file2
mydir/file3
mydir/mydir2/
mydir/mydir2/file21
mydir/mydir2/file22
mydir/mydir2/file23
</PRE>
<!-- END CODE //-->
<P>The tar command will show you what directories and files are being added. Does this
mean you have to add all of the files in your directories? No! You can use the
-w, or interactive option, and have tar ask you if you want each file added. This can be handy for
selectively backing up small directories, for example:
</P>
<!-- CODE //-->
<PRE>
# tar -cw mydir > mydir.tar
add mydir?y
add mydir/file1?n
add mydir/file2?y
add mydir/file3?n
add mydir/mydir2?y
add mydir/mydir2/file21?y
add mydir/mydir2/file22?n
add mydir/mydir2/file23?y
</PRE>
<!-- END CODE //-->
<P>Here I've left out file1, file3, and file22 from this archive. But how can you make sure?
One way is to use two tar options, -t to list an archive's contents, and
-f, to specify a tar archive to use, for example:
</P>
<!-- CODE SNIP //-->
<PRE>
# tar tf mydir.tar
mydir/
mydir/file2
mydir/mydir2/
mydir/mydir2/file21
mydir/mydir2/file23
</PRE>
<!-- END CODE SNIP //-->
<P>Note that the order of the options is important (like the
grep example), or tar will complain and quit. Now that you know how to create and see the contents of an archive, you
can learn how to extract the whole archive or a single file. To extract everything, you can
use the -x, or extract option with -f. Just so you know what's going on, include the
-v option, too:
</P>
<A NAME="PAGENUM-80"><P>Page 80</P></A>
<!-- CODE SNIP //-->
<PRE>
# tar -xvf mydir.tar
mydir/
mydir/file2
mydir/mydir2/
mydir/mydir2/file21
mydir/mydir2/file23
</PRE>
<!-- END CODE SNIP //-->
<P>If you want only a few files from your archive, you can again use the
-w option:
</P>
<!-- CODE //-->
<PRE>
# tar -xvwf mydir.tar
extract mydir/?y
mydir/
extract mydir/file2?y
mydir/file2
extract mydir/mydir2/?y
mydir/mydir2/
extract mydir/mydir2/file21?y
mydir/mydir2/file21
extract mydir/mydir2/file23?y
mydir/mydir2/file23
</PRE>
<!-- END CODE //-->
<P>Here, I've gone through the archive, interactively extracting files. If you want just a
single file from an archive, you can specify the file on the command line. For this example,
the original mydir has been removed, and I'm using an empty directory:
</P>
<!-- CODE //-->
<PRE>
# tar -xf mydir.tar mydir/mydir2/file23
# tree mydir
mydir
`-- mydir2
`-- file23
1 directory, 1 file
</PRE>
<!-- END CODE //-->
<P>As you can see, only one file was extracted. Be careful, though! The
tar command won't overwrite whole directories, but it will overwrite files with the same name.
</P>
<P>Try experimenting with tar before you start building archives. Some other features to
try are selectively deleting files from an archive, or adding a file to an existing archive,
which is something the next program, cpio, can do, too.
</P>
<H4><A NAME="ch05_ 18">
Creating cpio Archives
</A></H4>
<P>The cpio command copies files in and out of tar or
cpio archives. Because it is compatible with tar, this section won't go into all the details of how it works, but it has some
features that tar does not, such as
</P>
<UL>
<LI> Support for both cpio and tar archives
<LI> Support for a number of older tape formats
<LI> The ability to read filenames from a pipe (which you'll learn about in the
next hour)
</UL>
<P>Very few, if any, software packages for Linux are distributed in
cpio format. Chances are you won't run across any
cpio archives in your search for new software across the
Internet. But if you're interested in the details about
cpio, see its man page.
</P>
<A NAME="PAGENUM-81"><P>Page 81</P></A>
<H4><A NAME="ch05_ 19">
Compressing Files with the gzip Command
</A></H4>
<P>The gzip command compresses files. This program is not only handy for saving disk
space by compressing large, less often used files, but could be, in combination with
tar, the most popular compressed file format for Linux. You'll often find files with the
.tgz or .tar.gz format while searching for new Linux software across the Internet.
</P>
<P>You'll also find that much of the documentation under your
/usr/doc directory has been compressed with gzip. This can save a lot of space. According the Free Software
Foundation folks in gzip's manual page, gzip has a 60 to 70 percent compression rate for text files.
</P>
<P>The gzip command is easy to use. To compress a file (or tape archive), enter
</P>
<!-- CODE SNIP //-->
<PRE>
# gzip mydir.tar
</PRE>
<!-- END CODE SNIP //-->
<P>By default, gzip will compress your file, append a
.gz extension, and delete your original file. To uncompress your file, you can use
gzip's companion program gunzip, or gzip's -d (decompress) option. You'll have to make sure the file has a
.gz (or .Z, -gz, .z, -z, or _z) extension, or both
gzip and gunzip will complain. If you want to specify your own
extension, use the -S (suffix) option.
</P>
<P>The gzip command also handles zip, compress, or pack compressed files. If you want
more information during compression or decompression, you can use
-l, or list option, to see the compressed or decompressed sizes of files.
</P>
<P>Finally, gzip also has a helpful option, -t, to test the integrity
of a compressed file. If the file is okay, gzip reports nothing. If you need to see
OK, use -tv when you test a file.
</P>
<H4><A NAME="ch05_ 20">
Compressing Files with the compress Command
</A></H4>
<P>The compress command does just that—it compresses files. This is one of the
earlier compression utilities for the UNIX world.
</P>
<P>Files created with compress traditionally have a
.Z extension. To compress a file, enter
</P>
<!-- CODE SNIP //-->
<PRE>
# compress file
</PRE>
<!-- END CODE SNIP //-->
<P>To uncompress a file, use
</P>
<!-- CODE SNIP //-->
<PRE>
# uncompress file.Z
</PRE>
<!-- END CODE SNIP //-->
<P>Like gzip, you must specify a filename with a
.Z extension, or compress will complain.
</P>
<TABLE BGCOLOR=#FFFF99><TR><TD>JUST A MINUTE</TD></TR><TR><TD>
<BLOCKQUOTE>
Interested in other compression utilities for Linux? Look on your
system for zip, unzip, zipcloak, zipnote, zipsplit,
zless, zcat, znew, zmore, zcmp, pack, compact,
shar, unshar, or zforce. You might not find all of these installed.
</BLOCKQUOTE></TD></TR></TABLE>
<P><CENTER>
<a href="0075-0077.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0082-0082.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -