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
- # tar -cvf archiveName.tar file_to_packageOR_directory
and to extract the above package you use this
- # tar -xvf archiveName.tar -C Path/where/u/want/it/to/be/extracted
-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 http://unixhelp.ed.ac.uk/CGI/man-cgi?tar
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
- # tar -zcvf archive.tar.gz file_to_packageOR_directory
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
- # tar -zxvf archive.tar.gz -C Path/where/u/want/it/to/be/extracted
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.
- # tar -jcvf archive.tar.bz2 file_to_packageOR_directory
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
- # tar -jxvf archive.tar.bz2 -C Path/where/u/want/it/to/be/extracted
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