Loading
Lesson 03
Courses / Learn Unix Linux Bash Command Line Terminal Tutorial
Using the GZIP and BZIP2 Commands to Compress and Decompress a TAR Archive File in Linux

Video Transcript

Welcome to another lesson on Linux commands. In this lesson I'll teach you how to compress files in a Unix-based terminal. That is, you could try this in either Linux or MacOS, for instance. So I am going to cover three commands in this lesson. One of them is very, is the most popular probably for compression of files in Linux. That is Gzip. Another one is Bzip2. Bzip2 provides a better pressure ratio. And finally I'll cover the compressed command. It's probably not as widely used though. So let's get started. So in order for me to compress a file, typically in Linux we first archive using tar. So if you want to know how to use the tar command, I have a video for that. In any case, I'm assuming you already have a tarred archive file. In my case here I did a ls-l in my current directory and I have this file called archive.tar. Now I want to compress this file and turn it into, let's first do Gzip and turn it into what we usually use, the tar.gz extension. You can also shorten that to tgz. Doesn't really matter but that's what we're going to do here. So it's very simple. All you have to do is type the name of the command, in this case Gzip, followed by the name of the archive, the tar file. So I'm going to say archive.tar. And then I'm going to do ls-shel to see what I got. Now you can see what happened here was. The file archive.tar became another file now called archive.tar.gz. So we no longer have the tar file. Another thing to notice is the file size. Here I have 14,973 bytes for the compressed file. If you look at the original tar file it was 20,480. So it compressed the difference of these two, right? It's about a little over 5,000 bytes. Right? So that's Gzip. Now if you want to decompress it, how can we get the tar file back? We're going to use the g-on-zip command. g-on-zip, gunzip, and the name of the file, the tar.gz file. In this case archive.tar.gz. Now if you do ls-shel again you can see now we have archive.tar and there is no longer a tar.gz file. So we turn the compressed file back into the tar file and you notice the file size is the same as the original one we had before 20,480 bytes. And that's Gzip, gunzip. It's probably the most popular. Now let's have a look at another command to compress called bzip2. So essentially we're going to do the same thing. You notice the same pattern. We're going to say the name of the command bzip2 followed by the name of the tar file archive.tar. Let's have a look at the listing. Now we can see we created a turn.tar file into another file called archive.tar.bz2. Different extension there. And let's have a look at the file size here. Seems like this one, the file size became 15,387 bytes. Now that's a little over the one with gzip. Gzip had 14,973. So gzip did a better job in this case. All right. Now let's say we want to turn this back into tar file. We're going to follow the same pattern. Instead of saying bzip2, we're going to say be unzip to bunzip to bunzip to archive tar.bz2. And now do ls dash l again, we got archive tar. It's back there. There's no longer a file called tar.bz2. Notice the file size is the same as the original archive tar 20,480 bytes. And that's the bzip2. It was funny this example bzip2 did worse than gzip, but it is said that in some cases bzip2 might have greater compression ratios. So this example is very small, but it would be nice to see. You can try it out if you have massive files and see which one does a better job. Just try both of them. You can do gzip, g unzip to turn them back and note the don't forget to note the file size. And then you can do bzip2 name of your archive and then you'll note the file size and then you compare and depend on which one is better. You pick the one you want. Of course you want the one with the lowest file size because that means the compression ratio is better, meaning it compresses more and you'll save more bandwidth as you send the file around.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: