We'll now look at how to compress and uncompress files in Linux.
Now reducing file size with compression is not
only useful because it requires less storage space.
It's also useful because it helps us transfer
files from system to system much faster.
Most Linux systems will have at least three compression utilities,
preinstalled gzip, Bzip two and XZ.
Now compressing a file with these utilities is super easy.
Such commands would compress file one file two file three
and create compressed versions of these files
and automatically delete the original files after.
So you end up with file one dot GZ file two dot BZ two or file three dot XZ.
The compressed versions of your original files
to decompress these, we would type in GN zip
and then the file name
and the equivalent commands B and zip file name or UN XZ file name.
When file one dot GZ is decompressed, the opposite thing happens.
The original uncompressed file file one is recreated and
the compressed file one dot GZ gets deleted.
Now,
sometimes this automatic deletion of the
compressed or uncompressed file is undesired.
And that means that after we gzip file one,
maybe we still want file one to stick around.
Well, how could we do that?
Well,
remember we have an easy way to see extra command-line
options supported by any program with dash dash help.
And we noticed here that the dash dash keep option
or dash K for short is what we're looking for.
So to keep our original files around, we can use commands like
gzip dash dash, keep file one
or Bzip two dash dash, keep file two
or XZ dash dash, keep file three.
And if we ever need to take a look at the contents of a compressed file,
some commands support a dash dash list option.
Normally,
these three utilities are most often used for compression and decompression,
but you might occasionally encounter additional programs like zip.
One advantage of the zip utility is that it can also pack
and compress entire directories or multiple files in the same archive.
While utilities like gzip and others can only
create compressed data with a single file inside
to create an archive called archive dot zip and compressed file one within it.
We can use a command like this or
to pack and compress the entire pictures directory into archive dot zip.
We would type something like this and the dash R
recursively compresses every file and subdirectory in the pictures directory.
Now to unpack and decompress a dot zip file, we type in Unzip
archive dot zip.
As we noted previously,
gzip has no option to take a directory and jam it in a single archive.
It can pack multiple files in a single file. It can only compress a single file.
Zip supports both packing and compression.
Since utilities like gzip, Bzip two XZ et cetera don't do that.
They're often used in conjunction with the other utility called Tar.
Now, in the previous lecture,
we talked about packing a file into a tar archive using the tar command like this.
After we generate our Tar file, we can also compress it with any utility we want
with commands like this.
But instead of going through two steps,
make the Tar archive and then compress it.
We can tell Tar itself to do both things for us in one step or even better.
Tar has an option dash dash,
auto compress to automatically figure out what compression
utility to use based on the file name extension.
We choose for our archive dot GZ will make it compressed with gzip
dot BZ two will make it compressed with Bzip
two and dot XZ will make it compressed with XZ
when unpacking and decompressing.
Though we don't have to tell Tar what decompression utility to use.
It can figure that out all by itself. So a command like this would suffice.