Command Line Basics: Create And Extract Tarballs

November 14, 2011 by
Filed under: command line, HowTo, linux, Ubuntu 

In the Linux world, tarball refers to a compressed tar archive file. The most common type uses gzip compression and the file typically ends in tar.gz or .tgz. The tar command itself has its origin in Unix systems where is was used to save files to magnetic tape. The name tar stands for Tape ARchive.

The tarball is perhaps the most common way for program source code to be packaged for GNU/Linux systems.  The tar file can be used to package several files together in a single archive. That archive file can then be compressed with one of several compression algorithms.

Create a tarball

To create a tarball, it's good to have all of the files you want in the package to be contained in a directory. Although this is not necessary, it's convenient when you then go to extract the file later. That way the archive will be extracted to its own directory rather than dumping its contents into the current directory. You can then create a tarball from the command line like this:

tar -cvzf archive.tar.gz directory_to_archive

In the above command, tar is followed by four command parameters:

Affiliate Link
  • c: create an archive
  • v: verbose output (list the files that are being processed)
  • z: compress the archive with gzip compression
  • f: use a file archive instead of tape. The f parameter is immediately followed by the name of the archive file to create, that's why it's the last parameter used.

 Extract a tarball

Extracting a tarball is done in a similar fashion to creating one. First you'll want to place the tarball file in a directory where you would like to extract it. If you would like to inspect the contents of the archive before extracting it, you can do so with this command.

tar -tf archive.tar.gz

The t parameter is what tells tar to list the contents of the file.

Once you've got the archive placed in the directory you want it, you can extract it with:

tar -xvzf archive.tar.gz

In the above command, the x parameter is used to tell tar to extract the archive. The rest of the parameters have the same meaning as they had when creating an archive.

More information

There are many more advanced options available for working with tar. You can read the entire manual by viewing the tar manpage.

man tar

Comments

One Response to “Command Line Basics: Create And Extract Tarballs”

  1. Chris P says:

    often now archive files come in archive.tar.bz2 format and tar supports extraction of those as well, just add the j parameter to extract:

    tar -xvjf archive.tar.bz2

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>