Using 'tar' (file archive) and 'gzip' (file compression)


  

Available CEE UCL softeware(s) related to this topic:

  • tar (command-line mode)
  • xtar (X11)
  • gzip (command-line mode)
  • compress/uncompress (command-line mode)
  • zip/unzip (command-line mode)
  • lha (command-line mode)

When a big file(s) or directory(ies) is not used frequently but you still need to keep it in your account for future use, you may consider compressing it or archiving it or both to conserve disk space.

There are several common tools for archiving and compressing; "tar", "compress/uncompress", "gunzip(=gzip) " and "zip/unzip".

The "tar" (tape archiver) command is useful for storing or "archiving" a bunch of files and directories. It archives and extracts files to and from a single file called a "tarfile. " The "tape" in tar command originated from the "old days" when backup and archive were solely done using magnetic tape devices, but it doesn't have to be nowadays and it can be any file. (well, still tape backup is the most common means to do regular system backups).

The syntax for "tar" command is

$ tar [key] [name ...]

where key is specified by a plethora of options (see abridged list below and unabridged list in the man pages) and name is either the file name or device name (such as magnetic tapes, CD-R, WORM, MO disk, etc.). Here are some of the more commonly used keys:


tar key It will
c Creates a new tape
t Lists the contents of a tarfile without untarring a tarfile
f File. Use the [name] argument as the name of the tarfile instead of the default tape device.
v Turns verification on
x Extracts selected files. If no file argument is given the entire contents of the tar file is extracted

Here is some examples:

$ tar cvf oink.tar /home/wclinton/project

Create a tarfile that archives all files and subdirectories in "/home/wclinton"

$ tar tvf /dev/rmt0/jumbo.tar

List contents of a tarfile in a magnetic tape ("/dev/rmt0" is a standard name for a mounted tape drive)

$ tar xvfp ~/archive/noway.tar ~/project

Extract all files from "noway.tar" tarfile in "/home/wclinton/archive" directory to "/home/wclinton/project" directory. If "noway.tar" tarfile contains subdirectoies, they'll be also extracted under "/home/wclinton/project" directory.

It is a common practice to compress the resulting tarfile when archiving (=tarring) is completed. Compression is done by using either "gunzip (=gzip) " or "compress" command.

$ gzip -9 oink.tar

Compress "oink.tar" using gzip with a maximum compression. Resulting file will be "oink.tar.gz"

$ compress oink.tar

Compress "oink.tar" using compress command. Resulting file will be "oink.tar.Z"


"gzip" is better in compression speed and efficiency 
than "compress."  However, "compress" is a standard command 
comes with the Unix OS, whereas "gzip" is a GNU program 
you need to install.

Thus, if you're preparing a "gzipped tarfile" file(s) for others, make sure the person who will receive the "gzipped & tar'd " file" (or often called as "tarball") has "gzip" installed in one's system. (if not sure, use "compress" instead)

Now, here's how to uncompress and untar (=extract) a compressed and tarred archive. Here, I'm using the pipeline technique that we discussed previously.

$ gzip -cd oink.tar.gz | tar xpvf -

Decompress and extract all files/directories from "oink.tar.gz" to current directory

$ uncompress oink.tar.Z | tar xpvf -

Decompress and extract all files/directories from "oink.tar.Z" to current directory

Further details on "compress" can be found from man pages. For "gzip," type "gzip -h" for additional flags information.

zip/unzip compression method popular in wintel PCs is also available via "zip/unzip" command. Their usage and flags information can be also found by typing "zip -h" and "unzip -h."