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.
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.
Examples
To tar all .cc and .h files into a tar file named foo.tgz use:tar cvzf foo.tgz *.cc *.hThis 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 cps100will 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.tgzThis 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
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).