Archives and compression
Many day-to-day file operations can be completed using single files, directories, and wildcards; but learning how to use archives and compression is important for any user interested in backups, transmitting file sets to other users, or simply saving space. A number of UNIX utilities are built into the operating system for archiving and compressing data.
The most common method to group multiple files into a single file (an
archive) is to use the
tar is short for Tape Archiver, due to its
roots for use with backup tapes; but today, it's commonly used for
disk-to-disk operations. Try archiving what you've done so far in the
$ cd ~ $ tar cvf /tmp/tutorial.tar TUTORIAL $ ls /tmp/
You should now see a file called tutorial.tar in /tmp/. This file
contains both the directory TUTORIAL and the files contained in it.
This example uses the command-line options
create, which tells
tarto create a new archive.
vstands for verbose, telling
tarto show a listing of each file that goes into the archive.
fstands for file, telling
tarto write the archive to a file rather than a device.
Next, go into /tmp and extract the contents of the archive:
$ cd /tmp/ $ ls $ tar xvf tutorial.tar $ ls
Notice that the first
ls command shows
tutorial.tar, but it doesn't show a TUTORIAL directory. The second
ls command, after the
tar command, demonstrates that you now
have a TUTORIAL directory in /tmp/. Remove the
/tmp/TUTORIAL directory to tidy up:
$ rm -R /tmp/TUTORIAL
Once you have a tar file, you might want to compress it. A number of
compression options are available in most UNIX distributions, but this tutorial focuses on
gzip to create something commonly
called a tarball. A tarball is a tar file that has been
compressed. Go ahead and create a compressed version of
tutorial.tar so that it takes up less space:
$ gzip tutorial.tar $ ls
Your tutorial.tar archives are now compressed and renamed tutorial.tar.gz. This is a tarball. To uncompress it, type:
$ gzip -d tutorial.tar.gz
The result is your original uncompressed tar file, tutorial.tar.