Using tar to archive

The program tar (originally for tape archive) is useful for archiving and transmitting files. For example, you may want to 'tar up' all your work for a course on the acpub and save it to your own computer's disk drive so you don't run into quota problems. You might also want to submit (e.g., for cps 108 or cps 100) an entire directory at once rather than the individual files in the directory. The tar program is useful for these and other tasks and is simple to use.

You can see more information by reading the man page, type man tar The examples below are not meant to be exhaustive.

Note: on the acpub system you probably want to use /afs/acpub/projct/cps/bin/tar since it understands the z option. You can use gtar instead.

Create, Extract, See Contents

The tar program takes one of three funcion command line arguments (there are two others I won't talk about). (the other options are u for update and r for replace, see the man page for details).

Exactly one function argument, c, t, x, is used in conjunction with other command line arguments shown below. Again, these examples are not meant to be complete, just useful.

Compression, Verbose, File specified

In addition to a function command line argument the arguments below are useful. I usually use z and f all the time, and v when creating/extracting.


To tar all .cc and .h files into a tar file named foo.tgz use:
    tar cvzf foo.tgz *.cc *.h
This creates (c) a compressed (z) tar file named foo.tgz (f) and shows the files being stored into the tar file (v). The .tgz suffix is a convention for gzipped tar files, it's useful to use the convention since you'll know to use z to restore/extract.

It's often more useful to tar a directory (which tars all files and subdirectories recursively unless you specify otherwise). The nice part about tarring a directory is that it is untarred as a directory rather than as individual files.

   tar cvzf foo.tgz cps100
will tar the directory cps100 (and its files/subdirectories) into a tar file named foo.tgz.

To see a tar file's table of contents use:

   tar tzf foo.tgz

To extract the contents of a tar file use:

    tar xvzf foo.tgz
This untars/extracts (x) into the directory from which the command is invoked, and prints the files being extracted (v).

If you want to untar into a specified directory, change into that directory and then use tar. For example, to untar into a directory named newdir:

   mkdir newdir
   cd newdir
   tar xvzf ../foo.tgz

You can extract only one (or several) files if you know the name of the file. For example, to extract the file named from the tarfile foo.tgz:

   tar xvzf foo.tgz

Other Archiving/Compression Tools

Many PC/Mac programs will be able to restore files that have been archived using tar. For example, on Macs, the Stuffit Deluxe program can handle Unix tar files. On PCs, the pkunzip program will handle Unix tar files. This makes it possible to tar files up on acpub and then use ftp to bring them to your personal machine where you can store the tar files and restore when needed. Of course you can run Linux too.

The zip and unzip commands available on acpub and CS systems, are very useful replacements for tar. Zip/unzip programs are nearly standard on Windows 95/NT machines and zip will archive entire directory structures with the right options (type zip by itself for help).

Owen L. Astrachan
Last modified: Wed Jan 21 13:49:40 EST 1998