Breadcrumbs 
Learning >> Documentation >> Compression >> tar
 
Recent News
We're No 1!
We've reached the top! (Of Google!)
[ more ]
Firefox 3 Download Day
Firefox 3 has been officially released and is looking to set a world record for the most downloaded piece of software in 24hrs.
[ more ]
SSL Renewed
SSL cert has been renewed by the admin team.
[ more ]
tar
tar

The tar command creates and extracts from archives. An archive can hold a group of files. It gets its name from 'tape archive', because it was used to write files to tape in the olden days (and still is!).

# create an archive called archive.tar containing file1 and file2
$ tar -cf archive.tar file1 file2

# show what files are in archive.tar
$ tar -tvf archive.tar
file1
file2

# extract the files from archive.tar
$ tar -xvf archive.tar

The first letter in the options to tar usually specifies what you are trying to do. In the first example above we are trying to create the archive, so we use '-c'. The '-t' in the second example means you are attempting to list the contents of the archive. And the '-x' in the last example means you are trying to extract files from the archive. Tar requires you to specify at least one of these.

The -v optional switches on verbosity, which means tar lets you know what it's doing as it's doing it, which is useful for large archives and you want to monitor progress. The '-f' option is obligatory too, and tells tar that the .tar file we are dealing with immediately follows this option on the command line. This is necessary because tar has a lot of options and could otherwise get confused as to which was the .tar file.

For more information on the various options to tar, check out the slightly intimidating manual page.

$ man tar

Tar makes no attempt to compress the files -- all it does is archive them. This is a good idea, because mathematicians are constantly coming up with new and better compression tools. For example, the zip compression is quite poor compared to other tools now available. If tar was attached to one and only one compression tool (like .zip's archiving is) then it will eventually become obsolete. By not attaching itself to any one tool it can take full advantage of better compression tools as they are invented.

For convenience however, you can utilise one of these compression utilities (i.e. gzip) through the tar command. Although it's quite possible to first create the .tar file and then use gzip to compress it into .tar.gz it's easier to just do as follows:

# This will create archive.tar.gz which is a compressed version of archive.tar (containing file1 and file2)
# The extra z tells tar to compress it first
$ tar -czf archive.tar.gz file1 file2

# This will extract archive.tar.gz - the extra z tells tar to gunzip it first.
$ tar -xzf archive.tar.gz

Lets take a look at one of the most common compression tools for UNIX systems - gzip.