tar - UNIX Command

The UNIX command tar stands for Tape ARchive and is used to create, add files to, and extract files from, a 'tape archive'. In this context, a tape archive can be any kind of physical storage media, (e.g. tape, CD). It can also be an archive file, ('tar file), commonly used when transferring data via FTP . Finally, the archive can even be nothing at all, (see second example below).

As with all UNIX commands, a complete description of the command as it can be used on your UNIX system can be obtained thus:

% man tar

Having said that, here's a couple of tips related to the use of the tar command that I've found useful:


% tar cf - filename | gzip -9 > filename.tar.gz

This command creates a gzipped tarfile. It's helpful when you have a large amount of data to archive, but don't have enough disk space left to create an uncompressed tarfile as an intermediate stage.

What's it doing? Well, the tar command archives the given filename into STDOUT. This is piped as the input into the gzip command, (of course, you can substitute your favourite file compression command). Finally, the output of the gzip operation is redirected to the file filename.tar.gz.


% cd fromdir ; tar cf - . | (cd todir ; tar xfBp -)

This example uses the tar command to copy a directory hierarchy from one place, (fromdir), to another, (todir).

What's it doing? Well, first it moves to the source directory. The subsequent 'tar cf' command indicates that a tar file will be created. The name of the file is given as '-', i.e. STDOUT. The '.' indicates all files below fromdir are to be archived.

Next, the output, i.e. STDOUT, from 'tar cf' is piped as STDIN into the next command pair. The parentheses ensure that we have moved to the target directory before the second 'tar' command extracts the tar file. If you want to know the significance of the 'B' and 'p' modifiers, 'man tar' should reveal all!