fbpx Linux Compression Tools: gzip, bzip2 and xz – Which to Use | Certificação Linux

Linux Compression Tools: gzip, bzip2, and xz Compared

📅 01/01/2019 ⏱ 6 min ✍️ Uira Ribeiro
Linuxcommands
Linux Compression Tools: gzip, bzip2, and xz Compared
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

For greater efficiency and backup media economy, there is the data compression feature.

There are basically three Linux data compressors with different data compression algorithms. The first to appear was GZIP, then BZIP2 and finally XZ.

gzip and gunzip data compressor

The first widely used data compressor is gzip. It uses a compression algorithm called Lempel-Ziv. This technique finds duplicate characters in the input data. The second occurrence of characters is replaced by pointers to the previous reference, in the form of distance and length pairs. When compressing a file, gzip adds the .gz suffix.

To compress a file:

$ gzip arquivo

To decompress a file:

$ gzip –d arquivo.gz

Or

$ gunzip arquivo.gz

bzip2 and bunzip2 data compressor

The bzip2 compressor compacts files using the Burrows-Wheeler and Huffman algorithm. This technique operates on large data blocks. The larger the block size, the higher the compression rate achieved. It is considered better than conventional data compressors. When compressing a file, bzip2 adds the .bz2 suffix.

To compress a file:

$ bzip2 arquivo

To decompress a file:

$ bzip2 –d arquivo.bz2

Or

$ bunzip2 arquivo.bz2

There are some cases where the compressed file may become larger than the original file. This can happen if the algorithm used does not find occurrences to perform data compression and the compressor header is added to the original file.

xz and unxz data compressor

Additionally, we have the xz data compressor, which uses an algorithm similar to gzip. It produces files with the .xz or .lzma extension.

To compress a file:

$ xz arquivo

To decompress:

$ xz --decompress arquivo.xz 

Or

$ unxz arquivo.xz

To give you an idea of the difference between the three compressors gzip, bzip2 and xz, see the comparative example of a TAR package of a backup file from a website:

site.tar        9,8M    # arquivo sem compactaçãosite.tar.gz     2,6M    # arquivo compactado com gzipsite.tar.bz2    2,4M    # arquivo compactado com bzipsite.tar.xz     2,1M    # arquivo compactado com xz 

Combining Files with tarball

Tarball files are packages of files and directories that maintain the original directory and file structure in a tar file, with the possibility of data compression.

The command to package files and directories is tar. The name of this command comes from "Tape-ARchive". It reads files and directories and saves them to tape or file.

Along with the data, it saves important information such as the last modification, access permissions and others. This makes it capable of restoring the original state of the data.

The tar command options are not so optional after all. It takes at least two arguments:

  • options:  Tells what tar should do
  • [source]: If tar is used to make a backup, this parameter can be a file, a device, a directory to be copied;
  • [destination]: If the command is used for backup, this option will specify the destination for the data. It can be a tarball file or a device. If used to restore files, it will specify a tarball file and a device from which the data will be extracted.

First you must choose what tar should do through the options:

  • -c: Creates a new .tar file;
  • -u: Adds more files to the .tar file only if they are new or modified;
  • -r: Adds the specified files at the end of the .tar file;
  • -g: Creates an incremental backup;
  • -t: Lists the contents of a .tar file;
  • -x: Extracts files from a .tar file;

It also has auxiliary options:

  • -j: Uses bzip2 to compress and decompress .tar.bz2 files;
  • -J: Uses xz to compress and decompress .tar.xz files
  • -z: Uses gzip to compress and decompress .tar.gz files;
  • -v: Lists all processed files;
  • -f: Indicates that the destination is a file on disk, not a magnetic tape unit;

The tar options can be combined in a single parameter such as "cvzf".

Because it is a command that was originally made to read/write to tape, to create a tar archive or read a tar archive on disk, you must always use the "f" option.

Examples:

To save a certain directory /var/lib/mysql to a file /var/backup/mysql.tar.gz:

$ tar cvzf /var/backup/mysql.tar.gz /var/mysql

To extract the same package:

$ tar xvzf /var/backup/mysql.tar.gz –C / 

You can open the contents of a tarball file in two ways:

$ gzip –d arquivo.tar.gz

The gzip command uncompresses arquivo.tar.gz and removes the .gz suffix.

$ tar xvf arquivo.tar

The tar utility extracts the package contents.

We can also use simpler forms:

$ tar xvzf arquivo.tar.gz

Or

$ gzip –dc arquivo.tar.gz | tar xv

If the file is compressed with bzip2, it must be decompressed by bunzip2 or use the -d option of bzip2.

$ bzip2 –d arquivo.tar.bz2

Or

$ bunzip2 arquivo.tar.bz2

And

$ tar xvf arquivo.tar

In the case of files compressed with xz, you can use the xz command:

$ xz -d linux.tar.xz

Followed by:

$ tar xvf linux.tar

OR

$ tar xvJf linux.tar.xz

In the graphical environment, you can decompress and extract a tarball file without much effort, just by clicking on the file. This way Linux will invoke in the background the appropriate data compressor, along with tar to extract the data package in the current directory.

See the comparison between the compression performed by the gzip, bzip2 and xz compressors and an uncompressed tar file:

$ ls -1shS linux*
895M linux.tar
165M linux.tar.gz
126M linux.tar.bz2
105M linux-5.4.3.tar.xz

For the exam, it is recommended to memorize the following table:

ExtensionCompressor UsedTar Option
.tar.gzGzip$ tar xvzf arquivo.tar.gz
.tar.bz2Bzip2$ tar xvjf arquivo.tar.bz2
.tar.xzXz$ tar xvJf arquivo.tar.xz

Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.

Liked it? Share

 

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp