Monday, December 16, 2013

How to Compress and Decompress Files and Folders In Linux (tar & untar)

========================
Create and Extract .bz2 Files
========================

Introduction:

bzip2 and bunzip2 are file compression and decompression utilities. The bzip2 and bunzip2 utilities are newer than gzip and gunzip and are not as common yet, but they are rapidly gaining popularity.
The bzip2 utility is capable of greater compression ratios than gzip. Therefore, a bzip2 file can be 10-20% smaller than a gzip version of the same file. Usually,files that have been compressed by bzip2 will have a .bz2 extension.


Installing bzip2 in debian:

# apt-get install bzip2


To uncompress a tar.bz2 file:


# bunzip2 iRedMail-0.7.4.tar.bz2

Now iRedMail-0.7.4.tar.bz2 file is converted to iRedMail-0.7.4.tar. [bz2 will go away..only tar extention will remain]


To untar the tar file:

# tar -xvf iRedMail-0.7.4.tar

To uncompress only bz2 file not (tar.bz2) :

# tar -jxvf WebHTB_V2.9.bz2


===========================
Compressing a File Using bzip2:
===========================

To compress a file using bzip2:

# bzip2 imehedi.txt

To compress a folder using bzip2:

# tar -jcvf archive_name.tar.bz2 directory_to_compress {SYNTAX}

    Example:
            # tar -jcvf iRedMail-0.7.4.tar.bz2 iRedMail-0.7.4


========================
Create and Extract Zip Files
========================
Zip is probably the most commonly used archiving format out there today. Its biggest advantage is the fact that it is available on all operating system platforms such as Linux, Windows, and Mac OS, and generally supported out of the box. The downside of the zip format is that it does not offer the best level of compression. Tar.gz and tar.bz2 are far superior in that respect. Let’s move on to usage now.


To compress a directory with zip do the following:

    # zip -r archive_name.zip directory_to_compress {Syntax}

    Example:
           # zip -r imehedi.zip mehediDocs

To decompress or unzip a directory with zip do the following:

    # unzip archive_name.zip {Syntax}
   
    Example:
           # unzip imehedi.zip

=======================
Create and Extract tar Files
=======================

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. Tar is probably the Linux/UNIX
version of zip – quick and dirty.

To compress a directory with tar do the following:

            # tar -cvf archive_name.tar directory_to_compress {SYNTAX}
   
    Example:
            # tar -cvf imehedi.tar imehediDocs


To decompress or untar a directory with zip do the following:

   
         # tar -xvf archive_name.tar {Syntax}
   
    Example:
           # tar -xvf imehedi.tar


============================
Create and Extract TAR.GZ Files
============================

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 with tar.gz do the following:

           # tar -zcvf archive_name.tar.gz directory_to_compress {Syntax}

    Example:
           # tar -zcvf imehedi.tar.gz imehediDocs


To compress a directory with tar.gz do the following:

         # tar -zxvf archive_name.tar.gz {Syntax}

    Example:
         # tar -zxvf imehedi.tar.gz

No comments:

Post a Comment