Creating
tar Files
In its simplest form, the command to create a tarball is
$ tar cf {archive_name} {file_Spec}
c
says to “create” a tarball from the file(s) indicated by file_spec. f says to name the
tarball
archive_name. You will generally want to compress the tarball to save disk space
and download time. The easiest way to do so, using GNU
’s tar, is to add z to the option
list, which causes
tar to invoke another GNU utility, gzip, on the tarball to compress it,
as in the following:
$ tar czf {archive_name} {file_spec}
To illustrate, the CD-ROM that accompanies this book contains the source files used to
build the
tar utility itself (version 1.12, to be precise). First, create a tarball (you will
need to copy the contents of the file from the source code directory on the CD-ROM,
/source/37
, to your hard disk):
$ tar cf tar.tar *
We use the standard wildcard (*) to request that all files and directories in the current
directory be stored in the tarball. A quick
ls command shows that none of the source
files were removed, but that a new file, named, as requested,
tar.tar, was created:
total 3683
-rw-r--r-- 1 kwall users 11451 Apr 19 1997 ABOUT-NLS
-rw-r--r-- 1 kwall users 1040 Apr 19 1997 AC-PATCHES
-rw-r--r-- 1 kwall users 9656 Apr 23 1997 AM-PATCHES
-rw-r--r-- 1 kwall users 732 Apr 18 1997 AUTHORS
...
drwxr-xr-x 2 kwall users 1024 Apr 25 1997 src
-rw-r--r-- 1 kwall users 10 Apr 25 1997 stamp-h.in
-rw-r--r-- 1 kwall users 3256320 May 3 22:54 tar.tar
drwxr-xr-x 2 kwall users 1024 Apr 25 1997 tests
As you can see, the tarball is quite large. You can either run
gzip on the file separately,
or use
tar’s z option to reduce the file size and, thus, the download time:
$ tar czf tar.tar.gz *
$ ls -l tar.tar.gz
-rw-r--r-- 1 kwall users 2590545 May 3 23:42 tar.tar.gz
$ gzip tar.tar
$ ls -l tar.tar.gz
-rw-r--r-- 1 kwall users 1712020 May 3 23:41 tar.tar.gz
The compression option slows down
tar’s operation somewhat, but saves typing. On the
other hand, calling
gzip directly results in much better compression, but you have to
type an additional command to prepare your package for distribution. Hackers being lazy
critters,
Makefiles usually include a target such as dist that creates a gzipped tarball.