Linux How to Create and extract tar.gz and tar.bz2 archives

Hi Guys,

I guess this is a quick tip. Question is How we can create a tar.gz and tar.bz2 in Linux.

I use these commands so often that I thought I should share because it could be helpful information. I am not exactly a Linux Pro but more of an intermediate user so your input will be always welcomed.

Ok first I started using zip command in Linux a while back and loved it. It was quick and easy and was supported on many OS’s like Linux and windows or even Mac.

Compression with zip isn’t always great then comes tar

Now if you just use tar to compress a file that doesn’t mean that you are compressing that file or group of files. Its more like packaging a file or group of files and folders.

So basic usage of tar is shown below

and to extract the above package you use this


-c above means create
-x means extract

The size is pretty much the same when you tar your files because it is just creating a archive for you files.

Now lets jump into file compression using tar

tar has to be told what algorithm need to be used to  compress your files and directories. So to lets use GZip

If you take a look at man page

You will notice that flag -z can be used to create a GZipped file which

Lets change the above tar command that creates of package to the once below to create a Compressed Archive

[geshi lang=”text” nums=”1″ target=”_self” ]

Notice that we added -z and .gz in the file extension. I added .gz so that I am clear when extracting an archive what algo I will need to use to extract the above

So lets extract it

[geshi lang=”text” nums=”1″ target=”_self” ]

See that because now I am aware that its gzipped from the extension thus I added -z to my flags else it will fail to extract if different algo is used to try to extract this archive.

For me I use it a bit to often and for me it offers good compression.

Now we can use BZip to create of archive too as shown below

From the manual we saw that -j flag can be used to create a bzip archive.

So as you see that I’ve added .bz2 so that when I try to extract it I know what algo will be required to extract the above archive.

Lets extract it now. Keeping everything else the same, we just replace -z with -j

As you see its pretty simple and straight forward process to create and extract an archive.

I used bz2 on non production servers and .gz on production servers because of the CPU cost involved.

bz2 offers far superior compression than the latter

I hope this tip would help you.

Don’t forget to check the man page to master tar






Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.