< Zurück | Inhalt | Weiter >

1.3.11 The tar and zip Commands

The tar and zip commands allow you to pack data into an archive or extract it back. They provide lossless data compression (unlike some image compression algorithms) so that you get back out exactly what you put in, but it can take up less space when archived.8 Therefore tar and zip are often used for data backup, archival, and network transmission.

There are three basic actions that you can take with tar, and you can specify which action you want with a single letter9 in the arguments on the command line. You can either


8. Well, technically, tar doesn’t compress the data in the file, but it does provide a certain amount of “compression” by cutting off the tail ends of blocks of data; for example, a file of 37 bytes in its own file takes up 4K of disk space since disk blocks are allocated in “chunks” (not the technical term). When you tar together a whole bunch of files, those extra tail-end empty bytes are not used (except in the final block of the TAR file). So, for example, 10 files of 400 bytes could be packed into a single 4K file, instead of the 40K bytes they would occupy on the filesystem. So, while tar won’t compress the data inside the file (and thus is quite assuredly “lossless”) it does result in a smaller file.

9. Linux option strings always start with a “-”, right? Yes, except for tar. It seems there is always an exception to every rule. The newer versions of tar allow the leading minus sign, but can also work without it, for historical compatibility reasons. Early versions of UNIX only had single

c: Create an archive.

x: Extract from an archive.

t: Get a table of contents.

In addition, you’ll want to know these options:

f: The next parameter is the filename of the archive.

v: Provide more verbose output.

Using these options, Table 1.4 shows examples of each of the basic functions.

Now let’s do the same thing using the zip command (Table 1.5). There

are actually two commands here—one to compress the files into an archive (zip), and the other to reverse the process (unzip).

Table 1.4 Examples of the tar command




tar tvf packedup.tar

tar xvf packedup.tar

tar cvf packedup.tar mydir


Gives a table of contents, in long (or verbose) form. Without the v, all you get is the filenames; with the v you get additional information similar in format to the ls -l command.

Extracts all the files from the TAR file, creating them according to their specified pathname, assuming your user ID and file permissions allow it. Remove the v option if you don’t want to see each filename as the file is extracted.

Creates a TAR archive named packedup.tar from the mydir directory and its contents. Remove the v option if you don’t want to see each filename as the file is added to the archive.



letter options. Newer POSIX versions of UNIX and the GNU tools, which means all flavors of Linux, also support longer full-word options prefixed with a double minus, as in --extract instead of x or -x.

Table 1.5 Examples of the zip and unzip commands




unzip -l packedup.zip

unzip packedup.zip

zip -r packedup mydir


Gives a table of contents of the archive with some extra frill around the edges, like a count of the files in the archive.

Extracts all the files from the ZIP file, creating them according to their specified pathname, assuming your user ID and file permissions allow it. Add the quiet option with

-q if you would like unzip not to list each file as it unzips it.

Creates a ZIP archive named packedup.zip from the mydir directory and its contents. The -r tells zip to recursively descend into all the subdirectories, their subdirectories, and so on; otherwise, zip will just take the files at the first layer and go no deeper.



Since TAR and ZIP files can contain absolute as well as relative pathnames, it is a good idea to look at their contents (e.g., tar tvf file) before unpacking them, so that you know what is going to be written where.


There are many, many more options for tar and zip that we are not cover- ing here, but these are the most common in our experience, and they will give you a good start.

The tar and zip commands are also worth knowing about by a Java devel-

oper because of their relationship to JAR files. If you are working with Java you will soon run across the notion of a Java ARchive file, or JAR file. They are recognizable by name, ending in .jar. Certain Java tools are built to under- stand the internal format of JAR files. For Enterprise Java (J2EE) there are similar archives known as WAR files and EAR files. The command syntax for dealing with the jar command that builds these archives is very similar to the basic commands of tar. The internal format of a jar is the same as a ZIP file. In fact, most places where you can use a JAR file you can use a ZIP file as well. (You will see more about this when we discuss the standard Java tools in Section 5.11.)


Here’s one more handy example we know you’ll use:

find . -name '*.java' -print | zip allmysource -@

This command starts in the current directory (“.”) finding every file that ends in

.java and gives their names to zip which will read them from standard in in- stead of its argument list (told to do so with the -@ argument) and zip them all into an archive named allmysource.zip. To put it simply, it will zip up all your Java source files from the current directory on down.