Back to top

Linux – Tape Archive (tar)

Using TAR

TAR is an archive file format and is some version of the Tape ARchiving utility. It is an archiving program that is designed to store and extract files from an archive file known as a tar file, while preserving file system information such as permissions, dates, etc ...

It is important to know that there are many common file extensions for uncompressed and compressed tar archives. The following is a list of some of the more common extensions:

tar file:

  • .tar

gzipped tar file:

  • .tar.gz
  • .tgz
  • .tar.gzip

bzipped tar file:

  • .tar.bz2
  • .tar.bzip2
  • .tbz2
  • .tbz

tar file compressed with compress:

  • .tar.Z
  • .Z
  • .taz

Syntax

The general form used for command line tar is as follows:

tar [options] [optional functions] [name of the file(s) or directory(ies) to be archived]

This page will give you a brief introduction on how to perform three basic operations:

  • create or pack a tar file
  • extract or unpack a tar file
  • list the contents of a tar file

Examples:

To create or pack a tar file:

For an uncompressed tar file:

tar -cf packed_files.tar file_to_pack1 file_to_pack2 ...

If you decide to then compress a tar file, you can then do the following:

gzip packed_files.tar

If you want to pack and compress everything all at once, do the following:

tar -cf - file_to_pack1 file_to_pack2 ... | gzip -c > packed_files.tar.gz

To extract or unpack a tar file:

For an uncompressed tar file:

tar -xf file_to_unpack.tar

If you need to decompress an archive first, you need to do the following before running the command for an uncompressed tar file:

gunzip packed_files.tar.gz

If you want to decompress and unpack everything all at once, do the following:

gunzip -c packed_files.tar.gz | tar -xf -

Note: Tarbomb is the slang term used to refer to a tarball containing files that untar to the current directory instead of untarring to a directory of its own. This can be a problem if it overwrites files of the same name, etc ...

To prevent making tarbomb on extraction, always use the -t option before using the -x option (-tf as opposed to -ft).

To list the contents of a tar file:

tar -tvf file_to_list.tar

Note: To use bzip2 instead of bzip, simply replace the commands above with bzip2 where gzip is used and bunzip2 where gunzip is used.

Common Options

-c
to create a tar file, writing the file starts at the beginning.
-f
specifies the filename (which follows the f) used to tar into or to tar out from; see the examples below.
-t
table of contents, see the names of all files or those specified in other command line arguments.
-v
verbose output, show, e.g., during create or extract, the files being stored into or restored from the tar file.
-x
extract (restore) the contents of the tar file.
-z
use zip/gzip to compress the tar file or to read from a compressed tar file.

Additional Information

For more information, you can always consult the following man page(s):

  • man tar
  • man compress
  • man gzip
  • man bzip2
  • man zcat