Hello, the beautiful people.
And welcome back.
Now, in the last video, we put our files into a tar ball, and now we want to learn about how to compress
that table to save space on our hard drive.
Now, compression happens using compression algorithms, and there are two main compression algorithms
in use in the Linux world, and the first is the GZIP algorithm and the second is B zip b zip two.
And the main difference between them is usually GZIP is faster but has less compression power and b
zip.
Two on the other hand can generally compress files to a smaller size than GZIP, but does require more
computational time.
So both of these compression algorithms are really easy to use.
So let's just start by talking about GZIP.
So to compress a tarball using GZIP, all you need to do is type gzip and then the archive name.
So who are our archive data and press enter and we'll see that that file has been compressed in place
using the GZIP algorithm and we can see that it has.
If we use the LZ command here, we can see that the dot GZ file extension has automatically been added
and we can use the file command to confirm that.
Yep, this is indeed GZIP compressed data and it was archived tar and it tells you when it was last
modified as well.
So that's really cool.
We can see quite a lot of information about that.
Now let's take a look at the file size.
So if I do the LZ command with the L option, actually, no, that's just the L option.
We'll see that.
File one, file two and file three are 10,000 bytes in size.
But our archive, the compressed archive is only 23,000 bytes, so it's about 7000 bytes or seven kilobytes
less than what the files would have been originally.
So we actually have managed to do a compression of about 23%.
So that's not bad.
Now this will vary from file type to file type and what you're actually compressing.
But in this case, we've got a compression of 23% with GZIP.
So notice how compression is a two stage thing.
First we make a tarball and then we compress that tarball with a compression algorithm which in this
case was GZIP.
Okay, cool.
So how about reversing the GZIP?
How can we get the tar ball back?
Well, this is very easy.
All we need to do is just clear the screen.
G Unzip our archive.
GZ Okay, so you just use G unzip on whatever it was.
And when you do that, you see it's now back to a tar file.
And if we look, we can see that it's a tar file.
We're using the file command to archive tar and it tells you it's a POSIX tar archive.
Hooray.
Okay, so we've managed to undo the GZIP operation.
Okay, so what about B's zip two?
Well, B, zip two is a more heavy duty compression algorithm, and it takes more computational time
than GZIP, but usually not always, but usually returns more compressed files than you get with GZIP.
So it's a bit of a tradeoff.
Now let's compress our tarball with B's zip two and see what we get.
So it's very easy.
All we have to do is be zip to our archive tar and when we see that we now see that it's gone and got
the Z to file extension and if we use the file command on it, we can see that it says it's b zip to
compress data.
It doesn't tell you what it was before it was compressed.
So we lose a bit of that information.
But it has indeed been compressed and the file command knows that it has.
So how big is it?
Well, if we take a look with the URL option, we can just have a look right there and we can see that
this is 23,137 bytes.
So it's slightly larger than the G's version of 23,023 bytes.
Now, zip two is usually best saved until you have really large files.
Now, for these relatively small text files, it didn't do as much good to use be zip two.
But on a large file such as a video, you'll definitely see a large difference between the compression
results achieved by B's zip two and G zip.
Now to undo a B zip to compression, it's actually very easy.
All we've got to do is be unzip to and then whatever the b zip to archive is.
So in our case, this archive turned up easy too.
And when we press enter, we see that we've come back to the tar file and again we can use the file
command to check and we've got our tar archive back.
Now there are other compression algorithms out there, such as the x z compression algorithm.
And if you want to see a more in-depth comparison between the performance of G, Zip B's it too and
the Z algorithm, I put a link in the resources section for you to find out.
Now there is something that you should bear in mind, however, which is how do you create zip files?
Now, zip files are files that you know are commonly used on Windows or Mac and, you know, as compressed
archives.
Now they're not in.
I don't think they are as compressed as what you'll get with GZIP or zip too.
But they.
They are commonly used on other on other operating systems.
So if you want to create a zip file, it's actually a one step process.
You'll just type zip.
Okay?
Then you'll put the file.
So let's, let's call it our thing, zip, and then we'll put file one point txt file to task and file
three dot txt.
And when we press enter, we'll see that we now have a zip file in there.
So if I press file our file after thing, sorry, zip.
It tells us that it's a zip archive data and this is what you can use on other operating systems such
as Windows.
And you usually can't with g zipped files or b zip to compressed files.
They don't tend to work too well on other operating systems by default.
You tend to have to install other software like seven zip or winrar or whatever it might be to extract
them.
And of course you can just unzip that zip file and it'll ask you, do you want to extract them all?
And then you can extract them all.
This is just extra asking us because it was like, Oh, you might have to overwrite some stuff, but
that's no problem.
We just said yes, and that worked.
So if you want to make zip files, zip and unzip is the way to go.
So if we clear the screen now, so far we've been looking at compression as a two step process.
First, we've been creating a tar ball using the tar command and then compressing that tar ball using
either GZIP or BS it two or, you know, any other compression algorithm.
But is there any way to do it in one step?
Well, because creating backups is such a common thing to do, the answer is yes, you can actually
create a tarball and compress it using a compression algorithm straight from within the tar command.
So let's just delete our tarball for now in our zip folder.
So our to archive tar, we're going to remove that and our fingers zip.
So we're going to get rid of all, every archive that we've got and then we're going to take it right
from just the raw data here, these raw files straight to a compressed archive in one go.
Okay.
So to create the tab, to create the tar ball itself, how would we do that?
Well, what we do is we create we do tar CBF, so create verbose and allow it to accept files.
We're going to create our archive tar there we go and file 1 to 3.
So this is the way that we normally do it.
But now to compress the files, we just need to give it one more option.
So to do it with GZIP, you just give it the Z option.
And if, if we do that, that will compress using GZIP and we should remember that instead of naming
it as our archive.
TAR Because this is going to be a g zip file, we should add g z at the end as well just for convention
because it won't be added automatically when using this method when you run GZIP on it before it was
added automatically the GZ file extension.
But now because we're doing it all in one step, we have to provide the file name, so we need to add
it on there.
Just so you know, it's a bit easier to see what's going on.
And when we have when we press enter, we'll see that our archive GZ has been, has been created and
we can check that it is in fact a zip file using the file command and it says that it is GZIP compressed
data.
So that's super cool.
Okay, now how would you do it using B zip two.
Well, if we clear the screen b's it two is exactly the same.
Except instead of Z you just have J.
So if you press, if you have J and now remember, we're going to change our extension to B zip two.
And when we press enter, we'll see that our archive, zip too has been created and we can check that
that is indeed valid by using the file command, telling it to look at the b zip to archive and it tells
us that it's b zip two compressed data.
So that's how you can compress in one go.
How would we go about extracting from those compressed archives in one go?
Well, that's really easy as well.
Okay, let's delete them again.
Actually, no, it's not delete them because we'll need them.
Right.
So here's how would we normally extract from a tar file.
Well, how would normally do it, if you remember, is tar then x vrf and so x for extract, v for verbose
and f to accept a file and you'd give it the the, the tar file.
But let's try and extract the GZIP option first.
Let's delete these original files and let's try and extract the the GZIP option first.
So to extract the GZIP option we've got to do is type the Z again because Z is for GZIP.
Okay, so you had this the Z when compressing, you have the Z when extracting, all you've got to do
is change the C to an X and you're good to go.
So when we press that, we'll see that now we've extracted them all out again and we've got the data
back.
So if we delete these files here, if we just delete them there.
Now how about extracting using B's?
YP Well, you remember that J is for B's.
YP So instead of having Z, let's just put the J and then we just need to tell it what the archive is
and remember that it's a B's YP to archive, so we need to actually change the name.
So we are tar extracting verbose through b zip two and then giving it the file and.
There we are.
We've extracted using bees too.
So it's very, very easy.
As long as you can remember that Z is for GZIP.
J is for bees too.
You saw it and you can extract and compress all in one go.
Hurray.
So we've seen so much in the last two videos.
It's quite hilarious that a section on compression would take so long.
Right.
Anyway, let's have a quick recap.
So you first learned about the concept of a tar ball.
Now tar files or tar balls are just containers for you to store the files that you want to compress.
Now you can create tar balls using the tar command.
And tar balls do not really do any compression on their own.
That's the job of a compression algorithm.
The tar ball just contains the files and you saw how to add files into tar balls and how to extract
files from tar balls using the tar command.
And remember that there's a cheat sheet in the resources section for this video that will list out the
different options and different ways of using the tar command.
Now, once you have the tar ball in order to compress it, you need to use a compression algorithm so
tar balls can be compressed using a variety of different compression algorithms.
And we took a look at GZIP and Bee Zip two, which are common options on Linux, but the Z option is
another option.
We didn't discuss it here, but if you want to read more about the comparison between the different
algorithms, I've also put a link to a interesting blog post in the resources section for this video
for you to have a look at as well.
But with regards to GZIP and BBS, it to GZIP tends to be faster but give less compression, whereas
BS YP too tends to give more compression but take a bit more computation time.
So that's just something to bear in mind.
But by all means check out the link in the resources section for a more in-depth discussion.
You saw also how you can create zip files for compatibility with things like Windows or Mac using the
zip and unzip commands.
So that's something as well that you've got on record as well and you saw as well how to not just do
making the tarball and then compressing in two steps.
You saw how to do archiving and compression in just one step using a variety of shortcuts and all that
stuff is in the cheat sheet that you can find in the resources section for this video.
If you ever need a refresher, it's all very simple, and once you look at the cheat sheet, probably
you'll be able to see the pattern.
But it's really quite simple stuff once you've done it once or twice.
Okay, so all in all, you can now create your own archives and backups using the Linux, using the
Linux command line, which is super awesome because on this section all about learning how to use files.
Now when you've created your files, you've edited them, they've copied the paste them, you've moved
them, you've done whatever you want with them.
You can also create your own backups and save for later in a say in a space efficient way.
So well done, you.
Now in the next video, what we're going to be doing is we're going to be closing our discussion for
this section on files and the file system.
And I want to say that you've really come so very far in this section.
And the next video, I want to take the time to have a recap of what we've learned in this section and
also to celebrate your progress.
So well done you now for a summary of this section of the course, as well as some a deserved set of
praises.
I'll see you in the next video.