File Compression and Unpacking on Linux

This is a very complete article on handling compressed files that I found on one of the blogs I always access,the Always Update. In it you will understand everything about all file formats… I leave here my opinion about the preference for bz2 compaction, as it is not, fast and super strong.

When we talk about compressing and unpacking files within Linux systems, we usually think of something simple and that no one needs to know, here's a big mistake. A good Linux system administrator needs to have extensive knowledge, and this item is highly recommended, especially when we deal only with terminal. After all, in some cases there will be no graphical environment and for this, you need to be aware of how to compress or unpack the files on Linux.

When it comes to Linux we have several types of files, however the most famous are gzip and bzip2 and so the extensions that will be displayed would be respectively .gz and bz2.Among the two types of files we quote, bzip2 is the best option when it comes to file compression, it can make compressed files smaller than if gzip were used, but in case of emergency know that bzip2 takes a while to unpack or compress.

But before you go around compressing everything, know that large files or directories need to be compressed with .tar and only then can be compressed in gz or gz2.

Files .tar

Now, let's use a nomenclature which may be strange to some, it would be packaging, the term is used to define the act of gathering multiple files making them one, as long as it is one inside the other, which is also called a package file. Usually this final file has the same size corresponding to the size of each unified, or packaged, file.

Currently even after so long, the most used utility still remains the .tar.

How to package using .tar

Because it is recursive, the tar command can be used not only in applications, it can also package files from an entire directory. It would basically be what happens in the supermarket, a tray of eggs, meat and so many other things and we insert all of them inside a bag or box, at this point we can use the act of putting everything together in a single packaging package.

To package using the .tar see how simple it is. Let's say you want to package the /opt directory so here's what the command syntax would look like.

sudo tar cvf empacotamento_opt.tar /opt

And so our /opt directory packaging will have the size equivalent to the actual size of all the files contained there!

How to unpack using .tar

Without much to explain, unpacking is similar to packaging, the command is very similar.

sudo tar xvf empacotamento_opt.tar

And then you might think that in the syntax of the command it is possible to identify the xvf and the cvf that appear in the syntax composition, but let's understand that as well.

  • c -> the file or directory that will be packaged.
  • f -> Item required to manipulate the file, the letter "f" comes from file.
  • v -> With v verbose mode is activated, i.e. you will know everything that is happening, the output of what is being done will be displayed. You no longer need to use the '-' to specify the TAR options.
  • x -> is used to extract the file or directory.

Compressing with compress, gzip and bzip2

Unlike packaging, compression as the name already says serves to compress the compressed files or items, for the purpose of decreasing the file size, this is due to the fact that compression acts on repeated bit sequences and that they are present in a file or package.

Common and still widely used compactors are:

  • Compress
  • Gzip
  • Bzip2

For starters let's install the much talked about compress:

For Ubuntu, Debian, Linux Mint:

sudo apt install ncompress

For Fedora and derivatives:

sudo dnf install ncompress
Note: The package name is usually the same for any distribution, so we only mention two examples!

Identified the result of the compactions of each compactor

Although it seems redundant, and it is, you need to understand when a file is compressed with each item.

  • extension.Z – Compressed with compress
  • Extension .gz – Compressed with gzip
  • Extension .bz2 – Compressed with bzip2

And how to compress with Compress, gzip and bzip 2?

To compress run:

compress -c aquivo > file. z

For gzip run:

gzip -c file > file.gz

For bzip2 run:

bzip2 -c file >.bz2

And how to unpack with Compress, gzip and bzip 2?

To compress run:

uncompress file. z

For gzip run:

gzip -d file.gz

For bzip2 run:

bzip2 -dc file.bz2 > file

Compressing and packaging at the same time .tar.bz2 and .tar.gz

Let's use the example of the first packaging we empacotamento_opt.tar

With .tar.gz:

sudo tar czvf empacotamento_opt.tar.gz /opt

With .tar.bz2:

sudo tar cjvf empacotamento_opt.tar.bz2 /opt

Unpacking and unpacking at the same time .tar.bz2 and .tar.gz

With .tar.gz:

sudo tar xzvf empacotamento_opt.tar.gz

With .tar.bz2:

sudo tar xjvf empacotamento_opt.tar.bz2

And to be no different, let's then understand the options of the commands we use as examples:

  • c -> the file or directory that will be packaged.
  • f -> Item required to manipulate the file, the letter "f" comes from file.
  • v -> With v verbose mode is activated, i.e. you will know everything that is happening, the output of what is being done will be displayed. You no longer need to use the '-' to specify the TAR options.
  • x -> is used to extract the file or directory.
  • Z-> Indicates that it will be compressed with compress
  • j -> Indicates that it will be compressed with bzip2

Source: Always Update .com.br