What we do in life echoes in eternity!
Home About Me Diary

Using linux and tar to archive with examples

posted on: 20:50, October 26th , 2007

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

* c --- to create a tar file, writing the file starts at the beginning.

* t --- table of contents, see the names of all files or those specified in other command line arguments.

* x --- extract (restore) the contents of the tar file.

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

* f --- specifies the filename (which follows the f) used to tar into or to tar out from; see the examples below.

* z --- use zip/gzip to compress the tar file or to read from a compressed tar file.

* v --- verbose output, show, e.g., during create or extract, the files being stored into or restored from the tar file.

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 anagram.cc from the tarfile foo.tgz:

tar xvzf foo.tgz anagram.cc


Write a comment

*Security code:
*1+2 = (write in words ;)

All materials on this site are licensed under the following license: "Steal every piece of information you can get your hands on and run as fast as you can "