Using tar to archive files


TAR is the GNU Tape ARchive utility. It is used to pack the contents of multiple files or directories in an archive file called a tarball. Tar can preserve directory organization including file ownership, permissions, links, as well as directory structure. To save space you can optionally enable compression with gzip, bzip2, or another external program.

For these examples I’m going to be using this sample directory which contains a few files.

Lets create a tarball from this directory. Run the following command.

Lets go over these options.

-c create tarball
-v verbose
-f output file

To extract this tarball.

The new option ‘-x‘ instructs tar to extract the archive.

Add compression with gzip when creating the archive by including the -z option.

Extracting the archive is similar. Make sure to include the gzip option as well.

You can also use the bzip2 utility to compress the archive, use the -j option.

And extract it.

To preserve permissions use ‘-p‘ when creating as well as extracting.

You may have noticed the file extensions used on the archives are different. Here is a quick rundown on the naming conventions. Understanding the extensions allows you to quickly figure out how to extract it.

*.tar (uncompressed tar archive)
*.tgz or *.tar.gz (gzip compressed tar archive)
*.tbz or *.tar.bz2 (bzip2 compressed tar archive)

You want to avoid including files in the current working directory when creating a tarball. When extracted these files will be dumped into the user’s current working directory instead of its own subdirectory, potentially overwriting files and creating a big mess.

Another helpful option to use is ‘-C ‘. This will extract a tarball in a specific directory.

A good way to check for any problems with extraction or to see exactly a tarball contains is to execute tar with the -t option. This will list the contents of a tarball and show the location they will be extracted.

This will also help you avoid situations where a tarball might use an absolute path (path starting at root ‘/’) when extracting instead of a relative path (path that is relative to your current directory) like the previous examples. This is not necessarily a bad thing, but it can be dangerous if you don’t realize this before extracting, since an absolute path can install to anywhere on your system.