How to create and extract tar, tar.gz and tar.bz2 files in Linux using command line

Data compression techniques has been extremely useful to us over the years. Whether its a zip file containing images to be sent in a mail or a compressed data backup stored on a server, we use data compression to save valuable hard drive space or to make the downloading of files easier. One of the most common compression formats used in GNU/Linux and variants is tar.gz. A tar.gz file is nothing but a gzipped tar archive. These days users of GNU/Linux system seldom have to use the command line to create or extract tar.gz archives. There are compression formats out there which allow us to sometimes compress our data by 60% or more.


Here i will go through using some of these formats to compress and decompress files and directories on a Linux machine. We’ll cover the basic usage of zip, tar, tar.gz and the tar.bz2 formats. These are some of the most popular formats for compression used on Linux machines.

Tar is a very commonly used archiving format on Linux systems. The advantage with tar is that it consumes very little time and CPU to compress files, but the compression isn’t very much either.

To Compress a folder:
# tar –czvf foldername.tar.gz foldername

To Uncompress a tar file:
# tar –xzvf foldername.tar.gz

To View files within tar.gz:
# tar –tzvf foldername.tar.gz

To Create tar only:
# tar -cvf foldername.tar foldername

To Extract tar only:
# tar -xvf foldername.tar

To View tar only:
# tar -tvf foldername.tar

This format is my weapon of choice for most compression. It gives very good compression while not utilizing too much of the CPU while it is compressing the data.

To compress a directory use the following syntax:
# tar -zcvf archive_name.tar.gz directory_to_compress

To decompress an archive use the following syntax:
# tar -zxvf archive_name.tar.gz

This will extract the files in the archive_name.tar.gz archive in the current directory. Like with the tar format you can optionally extract the files to a different directory:
# tar -zxvf archive_name.tar.gz -C /tmp/extract_here/

This format has the best level of compression among all of the formats I’ve mentioned here. But this comes at a cost – in time and in CPU. Here’s how you compress a directory using tar.bz2:

# tar -jcvf archive_name.tar.bz2 directory_to_compress

This will extract the files in the archive_name.tar.bz2 archive in the current directory. To extract the files to a different directory use:
# tar -jxvf archive_name.tar.bz2 -C /tmp/extract_here/

Data compression is very handy particularly for backups. So if you have a shell script that takes a backup of your files on a regular basis you should think about using one of the compression formats you learned about here to shrink your backup size.

Learn Basic Linux Commands