The most common uses of Tar command are:

  • Create archive
    tar -cvf archive_name.tar dir_to_compress
  • Extract archive
    tar -xvf archive.tar
  • Show the Tar content ( … without expand the archive)
    tar -tf archive.tar

The meanings of the most common used option are:

  • c: create the archive
  • f: specify the file to extract or create
  • x: extract the archive
  • t: allow to show the archive contents
  • v: verbose

Don’t forget Tar is an utility that just archive the file, without reduce the file size.
For file compression we can use the gzip or bzip2. These two can be used for file compression but not archivation.
Considering that gzip and bzip2 can be used only for file compression but not for make archive, we can combine the use of Tar with these two just adding “z” for gzip or “j” for bzip2 in the Tar options.

In this way we can archive and compress directory.
Here the commands:

  • Compress archive with gzip (.tgz or tar.gz)
    tar -czvf archive_name.tgz dir_to_archive
  • Compress archive with bzip2 (.tar.bz2)
    tar -cjvf archive_name.bz2 dir_to_archive
  • Expand archive created with gzip (.tgz or tar.gz)
    tar -xvfz ./archive_name.tgz
  • Expand archive created with bzip2 (.tar.bz2)
    tar -xjvf ./archive_name.tar.bz2
  • List the compressed archive contents
    tar -ztf archive_name.tar.gz