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

For 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

For 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 Mon followed by the command ...


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Jaime Perea said

    Hello,

    Thank you very much for your posts, they are always useful.

    Perhaps it would be interesting to also mention xz, as it is also being used quite a bit. It ranks somewhere between bzip2 (slow, but compresses a lot) and gzip (fast, but less efficient). This at large ranges, because like everything ... it depends. The tars included in Debian / Ubuntu .deb files usually come compressed in the xz format.

    The way to use it is similar to the other sos commands.

  2.   Ernesto said

    Hello, I would like to ask that this be done but with tar.gz since it is the most used (in my opinion according to everything I download from the internet)

  3.   jolt2bolt said

    What do they say about more popular but multiplatform formats like .7z? They should name them too

  4.   omeza said

    Hi Jose, what happens with the tar.gz files is that you use another command which is tar and in this case the tar command by itself does not compress (or decompress) but is used to group (or ungroup) several files in one, this has integration with the gzip and bzip2 command with which you can compress and decompress.

    1.    Gonzalo said

      You are absolutely right, Ernesto, for a 7z free format that is making room for itself in Windows, replacing zip and rar, and they don't mention it?

  5.   a said

    Google com

  6.   usr said

    In the 21st century and still using commands to compress a simple file? This post is sad

    1.    usr/share said

      Well said, I don't see the point of using a command to compress a simple file

  7.   Katrin said

    Maybe it would be interesting too