How to create and extract zip, tar, tar.gz and tar.bz2 files in Linux ( bz2 and gz ) ( bzip2 and gzip )

tars

zip

Data compression 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. There are compression formats out there which allow us to sometimes compress our data by 60% or more. I’ll run you 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.

Before we delve into the usage of the formats I’d like to share some of my experience using the various formats of archiving. I’m talking about only a few data compression formats here, and there are many more out there. I’ve realized that I need two or three formats of compression that I’m comfortable using, and stick to them. The zip format is definitely one of them. This is because zip has become the de-facto standard choice for data compression, and it works on Windows as well. I use the zip format for files that I might need to share with Windows users. I like to use the tar.gz format for files that I would only use on my Mac and Linux machines.

ZIP

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

Here’s how you extract a zip archive:

# unzip archive_name.zip

TAR

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. Here’s how you compress a directory:

# tar -cvf archive_name.tar directory_to_compress

And to extract the archive:

# tar -xvf archive_name.tar.gz

This will extract the files in the archive_name.tar archive in the current directory. Like with the tar format you can optionally extract the files to a different directory:

# tar -xvf archive_name.tar -C /tmp/extract_here/

TAR.GZ

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/

TAR.BZ2

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.

Over time you will realize that there is a trade-off between the level of compression and the the time and CPU taken to compress. You will learn to judge where you need a quick but less effective compression, and when you need the compression to be of a high level and you can afford to wait a little while longer.

Create and Extract .bz2 and .gz 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

Installing bzip2 in ubuntu

sudo apt-get install bzip2

Uncompressing a bzip2 File Using bunzip2 in Debian

To uncompress a bzip2 file, execute the following command

#bunzip2 filename.txt.bz2 (where filename.txt.bz2 is the name of the file you wish to uncompress)

The result of this operation is a file called filename.txt. By default, bunzip2 will delete the filename.txt.bz2 file.

Compressing a File Using bzip2

To compress a file using bzip2, execute the following command:

#bzip2 filename.txt (where filename.txt is the name of the file you wish to compress)

The result of this operation is a file called filename.txt.bz2. By default, bzip2 will delete the filename.txt file.

Uncompressing a bzip2 File Using bunzip2 in Ubuntu

To uncompress a bzip2 file, execute the following command

sudo bunzip2 filename.txt.bz2 (where filename.txt.bz2 is the name of the file you wish to uncompress)

The result of this operation is a file called filename.txt. By default, bunzip2 will delete the filename.txt.bz2 file.

Compressing a File Using bzip2

To compress a file using bzip2, execute the following command:

sudo bzip2 filename.txt (where filename.txt is the name of the file you wish to compress)

The result of this operation is a file called filename.txt.bz2. By default, bzip2 will delete the filename.txt file.

Create and Extract .gz Files

Introduction

gzip and gunzip are GNU file compression and decompression utilities. Usually, files that have been compressed by gzip will have a .gz extension. However, sometimes you may see a file that has a .tgz extension. This is a TAR file that has been compressed by gzip. The .tgz extension is a shorthand version for the .tar.gz extension. This type of file must be uncompressed with gunzip before it can be untarred. However, there is a way to use the tar command to uncompress the file and untar it at the same time. For more information, see the tar: Tape Archive Files guide.

Installing gzip in Debian

#apt-get install gzip

Installing gzip in Ubuntu

sudo apt-get install gzip

Uncompressing a gzip File Using gunzip in Debian

To uncompress a gzip file, execute the following command:

#gunzip filename.txt.gz (where filename.txt.gz is the name of the file you wish to uncompress)

The result of this operation is a file called filename.txt. By default, gunzip will delete the filename.txt.gz file.

Compressing a File Using gzip

To compress a file using gzip, execute the following command:

#gzip filename.txt (where filename.txt is the name of the file you wish to compress)

The result of this operation is a file called filename.txt.gz. By default, gzip will delete the filename.txt file.

Uncompressing a gzip File Using gunzip in Ubuntu

To uncompress a gzip file, execute the following command:

sudo gunzip filename.txt.gz (where filename.txt.gz is the name of the file you wish to uncompress)

The result of this operation is a file called filename.txt. By default, gunzip will delete the filename.txt.gz file.

Compressing a File Using gzip

To compress a file using gzip, execute the following command:

sudo gzip filename.txt (where filename.txt is the name of the file you wish to compress)

The result of this operation is a file called filename.txt.gz. By default, gzip will delete the filename.txt file.

Advertisements

Posted on June 15, 2015, in Linux (Ubuntu/CentOS). Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: