Running Linux, 5th Edition - Matthias Kalle Dalheimer [237]
Earlier versions of gzip used .z (lowercase) instead of .gz as the compressed-filename extension. Because of the potential confusion with .Z, this was changed. At any rate, gunzip retains backward compatibility with a number of filename extensions and file types.
Using tar
tar is a general-purpose archiving utility capable of packing many files into a single archive file, while retaining information needed to restore the files fully, such as file permissions and ownership. The name tar stands for tape archive because the tool was originally used to archive files as backups on tape. However, use of tar is not at all restricted to making tape backups, as we'll see.
The format of the tar command is:
tar functionoptions files...
where function is a single letter indicating the operation to perform, options is a list of (single-letter) options to that function, and files is the list of files to pack or unpack in an archive. (Note that function is not separated from options by any space.)
function can be one of the following:
c
To create a new archive
x
To extract files from an archive
t
To list the contents of an archive
r
To append files to the end of an archive
u
To update files that are newer than those in the archive
d
To compare files in the archive to those in the filesystem
You'll rarely use most of these functions; the more commonly used are c, x, and t.
The most common options are
k
To keep any existing files when extracting—that is, to not overwrite any existing files that are contained within the tar file.
f filename
To specify that the tar file to be read or written is filename.
z
To specify that the data to be written to the tar file should be compressed or that the data in the tar file is compressed with gzip.
j
Like z, but uses bzip2 instead of gzip; works only with newer versions of tar. Some intermediate versions of tar used I instead; older ones don't support bzip2 at all.
v
To make tar show the files it is archiving or restoring. It is good practice to use this so that you can see what actually happens (unless, of course, you are writing shell scripts).
There are others, which we cover later in this section.
Although the tar syntax might appear complex at first, in practice it's quite simple. For example, say we have a directory named mt, containing these files:
rutabaga$ ls -l mt
total 37
-rw-r--r-- 1 root root 24 Sep 21 2004 Makefile
-rw-r--r-- 1 root root 847 Sep 21 2004 README
-rwxr-xr-x 1 root root 9220 Nov 16 19:03 mt
-rw-r--r-- 1 root root 2775 Aug 7 2004 mt.1
-rw-r--r-- 1 root root 6421 Aug 7 2004 mt.c
-rw-r--r-- 1 root root 3948 Nov 16 19:02 mt.o
-rw-r--r-- 1 root root 11204 Sep 5 2004 st_info.txt
We wish to pack the contents of this directory into a single tar archive. To do this, we use the command:
tar cf mt.tar mt
The first argument to tar is the function (here, c, for create) followed by any options. Here, we use the option f mt.tar to specify that the resulting tar archive be named mt.tar. The last argument is the name of the file or files to archive; in this case, we give the name of a directory, so tar packs all files in that directory into the archive.
Note that the first argument to tar must be the function letter and options. Because of this, there's no reason to use a hyphen (-) to precede the options as many Unix commands require. tar allows you to use a hyphen, as in:
tar -cf mt.tar mt
but it's really not necessary. In some versions of tar, the first letter must be the function, as in c, t, or x. In other versions, the order of letters does not matter.
The function letters as described here follow the so-called "old option style." There is also a newer "short option style" in which you precede the function options with a hyphen, and a "long option style" in which you use long option names with two hyphens. See the Info page for tar for more details if you are interested.
Be careful to remember the filename