Command Line Basics: Create And Extract Tarballs
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:
In the above command, tar is followed by four command parameters:
- 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.
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:
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.
There are many more advanced options available for working with tar. You can read the entire manual by viewing the tar manpage.