[Linux] Create and extract zip, tar, tar.gz and tar.bz2 in Linux (Ubuntu, Redhat, CentOS)

Linux has a variety of tools for working with compressed data. This article will describe how to use them, and why.

Compression programs look for patterns in the data, and then replace the original file with a file that describes those patterns. Nothing is lost--that description contains all the information needed to recreate the original file. The description will be smaller than the original file, but how much smaller will depend on the data itself and the compression scheme used.

1. ZIP
Compress:
zip -r archive_name.zip directory_to_compress
Decompress:
unzip archive_name.zip

2. TAR
Compress:
tar -cvf archive_name.tar directory_to_compress
Decompress:
tar -xvf archive_name.tar.gz
Option decompress: extract the files to a different directory:
tar -xvf archive_name.tar -C /tmp/extract_here/

3. TAR.GZ
Compress:
tar -zcvf archive_name.tar.gz directory_to_compress
Decompress: extract the files in the archive_name.tar.gz archive in the current directory
tar -zxvf archive_name.tar.gz
Option decompress:  extract the files to a different directory
tar -zxvf archive_name.tar.gz -C /tmp/extract_here/
gzip, btar
4. TAR.BZ2
Compress:
tar -jcvf archive_name.tar.bz2 directory_to_compress
Decompress:
tar -jxvf archive_name.tar.bz2 -C /tmp/extract_here/