-->

Previous | Table of Contents | Next

Page 78

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.

Creating Archives with the Tape Archive Command

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").

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).

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.

First, you'll create a directory with three files, and then create a subdirectory with another three files:


# 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

Now that you have built a directory, create a tar archive with this command:


# 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

Page 79

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:


# tar -cv mydir > mydir.tar

mydir/

mydir/file1

mydir/file2

mydir/file3

mydir/mydir2/

mydir/mydir2/file21

mydir/mydir2/file22

mydir/mydir2/file23

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:


# 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

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:


# tar tf mydir.tar

mydir/

mydir/file2

mydir/mydir2/

mydir/mydir2/file21

mydir/mydir2/file23

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:

Page 80


# tar -xvf mydir.tar

mydir/

mydir/file2

mydir/mydir2/

mydir/mydir2/file21

mydir/mydir2/file23

If you want only a few files from your archive, you can again use the -w option:


# 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

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:


# tar -xf mydir.tar mydir/mydir2/file23

# tree mydir

mydir

`-- mydir2

    `-- file23



1 directory, 1 file

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.

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.

Creating cpio Archives

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

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.

Page 81

Compressing Files with the gzip Command

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.

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.

The gzip command is easy to use. To compress a file (or tape archive), enter


# gzip mydir.tar

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.

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.

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.

Compressing Files with the compress Command

The compress command does just that—it compresses files. This is one of the earlier compression utilities for the UNIX world.

Files created with compress traditionally have a .Z extension. To compress a file, enter


# compress file

To uncompress a file, use


# uncompress file.Z

Like gzip, you must specify a filename with a .Z extension, or compress will complain.

JUST A MINUTE
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.

Previous | Table of Contents | Next