Compress files and folders

If you have been using linux for a few days, you see that it's almost impossible to go by without seeing a compress file. Here are the basic rules on how to create compressed folders .zip, .gz .tar / .tar.gz / .tar.bz2 .7z files

.zip

The zip format is pretty much straite forward. No fancy flags, simply the command and the file(s) to unzip
unzip somefile.zip

.gz

Gzip format was develop as a GNU project. It was created to replace the compress program in the unix systems. As zip, you don't need any fancy flags.
gunzip somefile.gz

.tar / .tar.gz / .tar.bz2

You might encounter some tarball will you search for datasets. These are basically a combination of the tar compression then compress using gzip. Here are two ways to decompress them.
gunzip < file.tar.gz | tar xvf -$ gunzip < file.tgz | tar xvf - 
or simpler
tar xvzf file.tar.gz
It is good to notice that tar can be combine with other things compression method like bzip2, xz, lzip, lzma and compress. With a tar.bz2, you will need to replace the z flag used earlier with the j flag.
tar xvjf file.tar.bz2

.7z

Now everyone has a friends that just know how to use 7zip on Windows OS and is affraid of the tar.gz extension. So this is how you could create a 7z folder on your Linux machine.
First install 7zip.
sudo apt-get install unrar (Debian like distros)
sudo yum install unrar (CentOs like distros)
sudo pacman -S p7zip (Archlinux like distros)
To compress files and folder, use the a flag
7z a compressFolder.7z *.jpg
7z a compressFolder.7z Documents/

Don't use it to backup documents in Linux

7zip does not store file owners and groups. To keep the owner / group, use tar.

To Backup

tar cf - directory | 7z a -si directory.tar.7z

To Restore

7z x -so directory.tar.7z | tar xf

Sources

  1. About Tech - Practical Examples For The Linux Unzip Command
  2. nixCraft - Linux / UNIX command to open .gz files
  3. nixCraft - Linux: tar Extract Files
  4. nixCraft - Open RAR File / Extract RAR Files Under Linux or UNIX
  5. Wikipedia - Gzip
  6. Wikipedia - Tar (computing)