In this article we are going to teach you how to compress and decompress files in 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 simple, users often search the Internet for a lot of how to perform these actions. I suppose that unlike other operating systems such as MacOS and Windows where very specific and intuitive graphic tools are used, in GNU / Linux there are usually more formats and different 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 the ones 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:
This generates a file named equal to the original with the extension .gz, in the previous example it would be document.txt.gz. Instead, to modify the output name for a specific one:
gzip -c documento.txt > nuevo_nombre.gz
To decompress what is already compressed, it is equally simple, although we can use two different commands with the same effect:
gzip -d documento.gz
And we will get the unzipped file without the .gz extension .
Working with bzip2
As for bzip2 , it 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:
With which a compressed document.txt.bz2 is obtained. 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
For more information you can use man followed by the command …