How to compress and decompress files in Linux

Press compression images

In this article we are going to teach you compress and decompress files from your favorite GNU / Linux distribution, all using commands from the console. It is an article aimed at beginners and in it we are not going to include the treatment of tarballs as in other tutorials, since it will only show how compression and decompression is done without packaging them with the wonderful tar tool.

Although compression and decompression is relatively easy, users often search the Internet for how to perform these actions. I suppose that unlike other operating systems such as MacOS and Windows where very specific and intuitive graphical tools are used, in GNU / Linux they are usually presented more formats and various tools for each of them, although there are also simple tools at the graphic level ...

For compression and decompression we are going to use two fundamental packages, since they are probably the most demanded formats and those that we come across most frequently when we are working on Unix-like systems. I am referring to gzip and bzip2.

Working with gzip

To compress with gzip, the format that we are going to handle is Lempel-Zi (LZ77), and not ZIP as such, since the name can lead to confusion. The name comes from GNU ZIP, and it was made as a substitute for the ZIP format, but it is not the same. I want to make that clear ... Well, to compress a file:

gzip documento.txt

That generates a file named equal to the original with the extension .gz, in the previous example it would be document.txt.gz. Instead, for modify the name output by a specific one:

gzip -c documento.txt > nuevo_nombre.gz

To decompress What is already compressed is equally simple, although we can use two different commands with the same effect:

gzip -d documento.gz

gunzip documento.gz

And we will get the file unzipped without .gz extension.

Working with bzip2

As for the bzip2, is similar to the previous program, but with a different compression algorithm called Burrows-Wheeler and Huffman coding. The extension we have in this case is .bz2. In order to compress a file, we just have to use:

bzip2 documento.txt

This results in a compressed document.txt.bz2. We can also vary the output name with the -c option:

bzip2 -c documento.txt > nombre.bz2

For decompression I would use the -d option of the bunzip2 tool which is an alias:

bzip2 -d documento.bz2

gunbzip2 documento.bz2

For more information you can use the Commission followed by the command ...