- [Instructor] In the previous video,
we looked at archiving with CPIO.
In this video, we're going to take a look
at how to compress data with gzip.
We'll also go through some additional features of gzip.
Gzip is a commonly used compression format
in the GNU Linux platform.
It's one of the utilities such as gzip,
gunzip, and zcat that handle gzip compression.
However, gzip can be applied only to
a single file or data stream.
This means that it cannot archive directories
and multiple files, hence we must first create
a tar archive and compress it with gzip.
Let's see how to operate with gzip.
Gzip can be used both to compress files
and decompress them back to the original.
In order to compress a file with gzip, we use this command.
So, we've got this error just because we have not
created a file in this directory, which we specified here.
Let's now extract a gzip compressed file with this command.
Now, in order to list out the properties
of a compressed file, we use this command.
The gzip command can read a file from stdin
and also write a compressed file into stdout.
Now read the data from stdin,
and output the compressed data to stdout in this way.
The minus-C option is used to specify output to stdout.
We can also specify the compression level for gzip
using minus-minus-fast, or the minus-minus-best option
to provide low- and high-compression ratios respectively.
Let's now dive into some additional information.
The gzip command is often used with other commands
and also has advanced options
to specify the compression ratio.
Let's see how to work with these features.
So, we first talk about gzip with tarball.
A gzipped tarball is basically
a tar archive compressed using gzip.
We can use two methods to create such tarballs.
The first method is this.
The minus-A option specifies that the compression format
should automatically be detected from the extension.
Alternatively, here's the second method.
First, create a tarball.
And then, compress the tarball in this way.
If many files are to be archived in a tarball
and need to be compressed,
we use the second method with few changes.
The issue with giving many files as a command argument
to tar is that it can accept only a limited number of files
from the command line.
In order to solve this issue, we can create a tar file
by adding files one by one using a loop
and an append option, minus-R, as here.
Now, in order to extract the gzipped tarball,
use this command.
In the above command, the minus-A option is used
to detect decompression format automatically.
Let's now look into zcat,
reading gzipped files without extracting.
Zcat is a command that can be used to dump an extracted file
from a .gz file to standard out
without manually extracting it.
The gz file remains as before,
but it will dump the extracted files
into standard out in this way.
Next is compression ratio.
We can specify the compression ratio,
which is available in range one to nine,
where one is the lowest but fastest,
nine is the best but slowest.
You can specify any ratio in that range as this.
This should give you a good balance
between compression, speed, and ratio.
Now, we move onto using bzip2.
Bzip2 is another commonly used tool
which is similar to gzip in function and syntax.
The only difference is that bzip2 offers
more effective compression than gzip
while taking more time than gzip.
To compress a file using bzip2.
First, extract the bzipped file as we've done here.
The way to compress and to extract from a tar.bz2 file
is similar to tar.gz discussed earlier,
where minus-J denotes that the archive is in bzip2 format.
Let's now explore using lzma.
Lzma is a compression tool which has
even better compression ratios than gzip and bzip2.
To compress a file using lzma, you can use this syntax.
Let's now extract an lzma-ed file, as in this way.
A tarball can be compressed by using the minus-minus-lzma
option passed to the tarball command
while archiving and extracting.
Now, in order to extract the tarball compressed with
lzma compression to a specified directory, use this command.
In this command, minus-X is used for extraction.
Minus-minus-lzma specifies the use of lzma
to decompress the resulting file,
or we could also use this one.
Great!
In this video, we've compressed data
by using the gzip command.
We also saw here some additional features of gzip.
In the next video, we'll look into
archiving and compressing with gzip.