3.4. Handling compressed files (tarballs)

The tar utility (tar is short for tape archive) allows you to compress files and directories into a single file called a tarball. Note that compressing, listing, and extracting all use the same command (tar), but the options used differ depending on which action you wish to perform.

Tarballs are typically compressed using GNU zip, or gzip, resulting in their file extension of .tar.gz or .tgz. However, some tarballs are created with the newer bzip2 compression, which results in a smaller file size. Their file extension then becomes .tar.bz2 or .tbz. The commands in the table below use gzip. If you'd prefer to use bzip2, you can use the commands below, replacing z with j among the command options (so that, for example, -czf becomes -cjf) and replacing .tar.gz with .tar.bz2.

Like with all commands, you can find more information by checking man tar, but since the version of tar used on Linux is a GNU product, you can also find information by typing info tar.

Table 3.4. Commands for handling compressed files (tarballs)[28]

CommandAction

tar -czf foo.tar.gz file_list

Creates a tarball called foo.tar.gz from the list of files and directories (separated by spaces) in file_list

tar -tzf foo.tar.gz

Lists the files and directories in foo.tar.gz (does not extract them)

tar -xzf foo.tar.gz

Extracts the contents of foo.tar.gz and places them in the current directory

[28] Although, when used with the options above, tar will be silent (that is, it won't provide any message to the user unless there's a problem, as mentioned in Section 1.5, “Key differences between Windows/Mac OS X and Linux”), you can use the -v option for verbose output, as in tar -czvf or tar -xzvf; when used as tar -tzvf, a “long listing” output like that in ls -l is used.



Back to Guide main page