Archives and their manipulation

Archives files (.tar) allow to group together files and folder and their respective attributes in a single file.

You have to note that the options of tar utility can be passed without the habitual preceding dash -.

General options

The f option (like file) will be used to select the archive file to use for the operation. If not specified, the data are written directly on the STDOUT (creation) and read from STDIN (extraction).

During the creation and extraction of archives the v option (like verbose) allow the display of files that will be included/extracted by tar.

Creation

To create the archive, you need to use the c option (like create). Therefore to archive the test folder in test.tar:

$ tar cvf test.tar test

Extraction

The extraction itself use the x option (like in extract). Therefore to extract the test.tar archive :

$ tar xvf test.tar

Compressed archives

gzip compressed archives

The majority of archives are compressed and had the extension .tar.gz. They are archive files (.tar) that are then compressed by the gzip utility (.gz). Archive creation and extraction are done in two successive steps. To facilitate the use of compression, tar can directly use gzip with the z option.

To compress at creation our folder in a compress archive test.tar.gz:

$ tar czvf test.tar.gz test

And to uncompress it:

$ tar xzvf test.tar.gz

Other compressions

The archive files can be compressed by several compression algorithm. However, tar allow easily the use of some common algorithms by simply passing an option as for gzip:

  • bzip2, with j option (extension .bzip2)
  • lzma, with J option (extension .xz)
  • lzip, with --lzip option (extension .lzip)

Automatic detection of compression algorithm

To remember the different options for the different compression algorithms is not always easy. tar as a very nice option (a) that allows automatically to determine the right algorithm depending on the extension of the file.

Therefore to easily compress with gzip:

$ tar cavf test.tar.gz test

Or with bzip2:

$ tar cavf test.tar.bzip2 test