Home Downloads Comparing Linux compression tools: gzip, bzip2, and xz

Comparing Linux compression tools: gzip, bzip2, and xz

This guide delves into the functionalities and comparisons of gzip, bzip2, and xz, three widely-used compression tools in Linux. Learn about their compression ratios, speed, and practical applications to choose the most suitable tool for your file compression and archival needs in various Linux environments.

by Divya Kiran Kumar
gzip bzip2 and xz linux compression tools

In the Linux ecosystem, effective file management often requires the use of compression tools, each with its own set of strengths and ideal use cases. Among these, gzip, bzip2, and xz stand out as the most commonly used utilities, each catering to different requirements in terms of compression ratio, speed, and resource usage. Understanding the nuances of these tools is not just a technical necessity but also a practical skill, helping users navigate through tasks ranging from quick file compressions to efficient archiving.

In this article, we delve into the specifics of gzip, bzip2, and xz, comparing their algorithms, performance, and typical use cases. This exploration aims to equip you with the knowledge to make an informed decision about which tool to use in various scenarios, enhancing your ability to handle files efficiently in the Linux environment.

Understanding file compression in Linux

Before we jump into the tools, let’s understand why compression is essential. File compression reduces the size of files, making them easier to store and faster to transfer. It’s particularly vital when dealing with large datasets, backups, or when bandwidth is limited.

Installation steps for gzip, bzip2, and xz on various Linux distributions

The installation of gzip, bzip2, and xz varies slightly across different Linux distributions. Below, I’ll outline the steps for a few popular ones: Ubuntu/Debian, Fedora, and Arch Linux. It’s worth noting that in many distributions, these tools are installed by default.

Installing on Ubuntu/Debian

Ubuntu and Debian, being closely related, share similar installation commands using apt-get.


sudo apt-get update
sudo apt-get install gzip


sudo apt-get update
sudo apt-get install bzip2


sudo apt-get update
sudo apt-get install xz-utils

Installing on Fedora

Fedora uses the dnf package manager, which simplifies the installation process.


Usually pre-installed, but if needed:

sudo dnf install gzip


Also typically pre-installed, but can be installed via:

sudo dnf install bzip2


Likewise, it’s generally pre-installed, but if required:

sudo dnf install xz

Installing on Arch Linux

Arch Linux uses the pacman package manager. As with Fedora, these tools are usually installed by default, but here’s how you can install them if necessary.


sudo pacman -Sy gzip


sudo pacman -Sy bzip2


sudo pacman -Sy xz

Checking installation

After installation, you can check if the tools are installed correctly by checking their versions:

gzip --version
bzip2 --version
xz --version

This will also give you a glimpse of other information like license details, authors, etc.

Example output for gzip

$ gzip --version
gzip 1.10
Copyright (C) 2007-2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Paul Eggert, Jean-loup Gailly, and Mark Adler.

Example output for bzip2

$ bzip2 --version
bzip2, a block-sorting file compressor.  Version 1.0.8, 13-Jul-2019.
Copyright (C) 1996-2019 by Julian Seward.
This program is released under the terms of the license contained
in the file LICENSE.

Example output for xz

$ xz --version
xz (XZ Utils) 5.2.4
liblzma 5.2.4
Copyright (C) 2009-2019 Tukaani Development Team
This program is provided "as is" without any warranty.

Let’s now delve into each of these compression tools in detail.

Gzip: the fast and reliable

gzip (GNU zip) is like an old friend in the Linux world. It uses the Lempel-Ziv coding (LZ77) algorithm and is known for its speed and reliability. It’s my go-to when I need to compress something quickly without thinking too much about the compression ratio.

Syntax of gzip

The basic syntax is:

gzip [options] [file]

To compress a file, simply use:

gzip filename

This replaces the original file with a compressed version ending in .gz.

Example output

Let’s say we have a file named data.txt. After running gzip data.txt, the output will be:

-rw-r--r-- 1 user user 10240 Nov 24 09:00 data.txt.gz

The original data.txt is gone, replaced by data.txt.gz.

Decompressing with gzip

To decompress, use:

gunzip filename.gz


gzip -d filename.gz

gzip command options

The gzip command comes with a variety of options that allow you to customize its behavior. Here’s a rundown of some of the most commonly used options:

  1. -d or –decompress: Decompresses the compressed files. This option is synonymous with the gunzip command.
  2. -k or –keep: Keeps (does not delete) the input files during compression or decompression.
  3. -l or –list: Lists the compression ratio and other details for specified gzip files.
  4. -c or –stdout: Outputs to standard output (stdout), keeping the original files unchanged. This is useful for piping.
  5. -r or –recursive: Recursively compresses or decompresses files in directories and subdirectories.
  6. -f or –force: Forces compression or decompression and overwrites any existing output files.
  7. -t or –test: Tests the compressed file integrity.
  8. -v or –verbose: Provides verbose output, showing the original and compressed file sizes and the compression ratio.
  9. -1 or –fast: Compresses faster, but with less compression (least compression).
  10. -9 or –best: Compresses slower, but with more compression (best compression).
  11. -n or –no-name: When compressing, do not save the original file name and timestamp; when decompressing, do not restore the original file name and timestamp (if present in the compressed file).
  12. -N or –name: When compressing, save the original file name and timestamp in the compressed file; when decompressing, restore the original file name and timestamp (default).

Example usage

  • To compress a file with maximum compression:
    gzip -9 filename
  • To decompress a file while keeping the original:
    gzip -dk filename.gz
  • To list the details of a compressed file:
    gzip -l filename.gz

These options enhance the flexibility and utility of gzip, making it suitable for a wide range of tasks in file compression and decompression.

Bzip2: the balance master

bzip2 strikes a balance between speed and compression ratio. It uses the Burrows-Wheeler block sorting text compression algorithm and Huffman coding, making it more efficient than gzip in terms of compression ratio, but a bit slower.

Syntax of bzip2

The basic syntax is:

bzip2 [options] [file]

To compress a file:

bzip2 filename

This replaces the original file with a .bz2 extension.

Example output

Compressing data.txt with bzip2 data.txt gives:

-rw-r--r-- 1 user user 9200 Nov 24 09:05 data.txt.bz2

Notice the smaller size compared to gzip.

Decompressing with bzip2

To decompress, use:

bunzip2 filename.bz2


bzip2 -d filename.bz2

bzip2 command options

Just like gzip, bzip2 also offers a variety of options for customizing its compression and decompression processes. Here’s an overview of some commonly used options in bzip2:

  1. -d or –decompress: This option is used to decompress files. You can also use bunzip2 for the same purpose.
  2. -z or –compress: Forces compression, even if the operation results in a larger file. This is the default behavior when no operation mode is specified.
  3. -k or –keep: Keeps (does not delete) the input files during compression or decompression.
  4. -f or –force: Forces the compression or decompression. This is useful when the output file already exists or the input files are in use.
  5. -t or –test: Tests the integrity of the compressed file without decompressing it.
  6. -v or –verbose: Provides verbose output, showing the compression ratio and any warnings.
  7. -c or –stdout: Writes output to standard output (stdout) and keeps the original files unchanged. This is useful for piping.
  8. -L or –license: Displays the software version and license information.
  9. -1 through -9: Adjusts the block size to use for compression, with -1 giving the smallest block size (and fastest compression with least compression) and -9 the largest block size (slowest compression with best compression). The default block size is -9.

Example usage

  • To compress a file with default settings:
    bzip2 filename
  • To decompress a file while keeping the original:
    bzip2 -dk filename.bz2
  • To compress a file with the fastest compression:
    bzip2 -1 filename
  • To test the integrity of a compressed file:
    bzip2 -tv filename.bz2

The options provided by bzip2 allow users to balance between compression speed and ratio, manage file handling during compression/decompression processes, and ensure the integrity of compressed data.

Xz: the compression powerhouse

xz is relatively newer and uses the LZMA/LZMA2 compression algorithm. It offers the highest compression ratio but can be slower and more resource-intensive. I use xz for archiving or when I have ample time and resources for compression.

Syntax of xz

The basic syntax is:

xz [options] [file]

To compress a file:

xz filename

The original file is replaced with a .xz file.

Example output

Compressing data.txt with xz data.txt results in:

-rw-r--r-- 1 user user 8800 Nov 24 09:10 data.txt.xz

The file size is even smaller than bzip2.

Decompressing with xz

To decompress, use:

unxz filename.xz


xz -d filename.xz

xz command options

xz is a powerful compression tool with a range of options that allow for fine-tuning of its behavior. Here are some of the key options you can use with xz:

  1. -d, –decompress: Decompresses files. This is equivalent to using the unxz command.
  2. -z, –compress: Forces compression, which is the default action if neither compression nor decompression is specified.
  3. -k, –keep: Keeps the original files unaltered during compression or decompression.
  4. -f, –force: Forces the compression or decompression, overwriting existing output files and compressing or decompressing files with multiple links.
  5. -t, –test: Tests the integrity of the compressed file without decompressing it.
  6. -c, –stdout, –to-stdout: Writes the output to standard output (stdout), which is useful for piping and combining with other commands.
  7. -l, –list: Lists information about .xz files, such as compression ratios.
  8. -q, –quiet: Reduces the verbosity of information, useful for scripts and batch operations.
  9. -v, –verbose: Increases the verbosity of information, showing progress and compression ratios.
  10. -0 to -9: Specifies the compression level, with -0 being the fastest and least compressive, and -9 being the slowest and most compressive. The default level is -6.
  11. -e, –extreme: Tries to improve the compression ratio by using more CPU time. This can be used in conjunction with the compression level options (-0 to -9).
  12. –threads=[0-9]: Specifies the number of worker threads to use. Setting it to 0 (the default) adapts the number of threads to the system.

Example usage

  • To compress a file with default settings:
    xz filename
  • To decompress a file while keeping the original:
    xz -dk filename.xz
  • To compress a file with the fastest setting:
    xz -0 filename
  • To list the details of a compressed file:
    xz -l filename.xz

The xz command’s options provide flexibility for managing the balance between compression level and resource usage, making it a suitable choice for various scenarios, from quick compressions to maximum space savings.

Personal preference and use cases: gzip vs. bzip2 vs.xz

When it comes to choosing between gzip, bzip2, and xz, my preferences are influenced by both technical nuances and practical scenarios. Let’s delve deeper into when and why I prefer one over the others, considering factors like compression ratio, speed, CPU usage, and compatibility.

When I lean towards gzip

  • Quick compression tasks: For everyday tasks like compressing logs or simple backups where time is more critical than space, gzip is my go-to. Its speed outshines its relatively lower compression ratio.
  • Scripting and piping: In shell scripts, especially when working with pipes, gzip‘s speed and straightforward functionality make it highly efficient. For instance, piping a tar output directly to gzip for quick archiving is something I do often.
  • Compatibility concerns: gzip is ubiquitously supported across various platforms and systems. When I’m working in environments where compatibility could be an issue (like older systems or cross-platform tasks), gzip ensures seamless integration.

Bzip2 for the balanced approach

  • Moderate compression needs: When I have files where the compression ratio matters more, but I can’t afford significant time or CPU overhead, bzip2 strikes the perfect balance. It works great for slightly larger datasets where space savings can be substantial yet doesn’t bog down the system.
  • Network transfers: For sending files over the network where bandwidth is a constraint but I have some time to spare, bzip2‘s better compression ratio reduces transfer time and costs.

Choosing xz for maximum compression

  • Archival purposes: When archiving critical data where space saving is paramount, xz is unbeatable. Its superior compression ratio, despite the longer time and higher CPU usage, is a trade-off I’m willing to make for long-term storage.
  • Distributing software packages: In software distribution, where the size of the package can significantly impact downloading time and storage, xz is increasingly becoming the standard, especially in the Linux ecosystem. Its high compression ratio makes large software packages more manageable.
  • CPU-intensive environments: In situations where CPU resources are not a bottleneck (like overnight batch processing or on powerful servers), I prefer xz for its efficient compression, despite its CPU-intensive nature.

Technical considerations

  • Compression ratio vs. time: gzip is about speed, bzip2 offers a middle ground, and xz excels in compression ratio. When deciding, I weigh the importance of time against space.
  • Resource usage: gzip is less CPU-intensive compared to bzip2 and especially xz. In resource-constrained environments, gzip often emerges as the practical choice.
  • File integrity and recovery: gzip and bzip2 are less resilient against file corruption compared to xz. When compressing very large files or critical data, xz‘s robustness adds an extra layer of security.

Overall, my choice among these tools is driven by a combination of factors including compression needs, time constraints, system resources, and the specific context of use. While gzip wins for quick and light tasks, bzip2 fits in for a more balanced approach, and xz stands out for scenarios where compression efficiency is the top priority.

Here’s a brief comparison table that outlines the key characteristics of gzip, bzip2, and xz:

Feature gzip bzip2 xz
Algorithm LZ77 (Lempel-Ziv) Burrows-Wheeler block sorting & Huffman coding LZMA/LZMA2
Compression ratio Good Better Best
Speed Fast Moderate Slow
CPU usage Low Moderate High
File extension .gz .bz2 .xz
Resilience to corruption Low Moderate High
Popularity/support Very High High Increasingly High
Typical use case Quick tasks, logs, small-size files Balanced tasks, moderate-size files Large files, archival, software distribution
Decompression speed Very Fast Slow Moderate


  • Compression ratio: How effectively the tool reduces file size. xz typically achieves the highest compression ratio, making it ideal for space-saving.
  • Speed: Refers to how quickly the tool compresses and decompresses files. gzip is known for its speed, making it suitable for tasks where time is a constraint.
  • CPU usage: The amount of CPU resources the tool uses. xz is more CPU-intensive due to its complex compression algorithm.
  • Resilience to corruption: How well the compressed file can recover from data corruption. xz offers better resilience.
  • Popularity/support: Indicates how widely used and supported the tool is in the Linux community.
  • Typical use case: Common scenarios where each tool is preferred, based on its features and performance.


The choice between gzip, bzip2, and xz for file compression in Linux depends on a nuanced balance of factors like compression ratio, speed, CPU usage, and the specific context of your needs. gzip stands out for its speed and widespread support, making it ideal for quick compression tasks and scenarios where compatibility is key. bzip2, with its better compression ratio and moderate speed, serves well for tasks that require a balance between file size reduction and resource usage.

On the other hand, xz shines in situations where maximum compression is crucial, such as for archiving large files or distributing software, despite its slower speed and higher CPU demand. Each tool has its unique strengths and ideal use cases, and understanding these can greatly enhance your efficiency and effectiveness in managing files in the Linux environment.

You may also like

Leave a Comment



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us


©2016-2023 FOSS LINUX



“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.