How to Compress Files Faster with Pigz Tool in Linux

Written by Mark Adler, Pigz is an acronym for Parallel Implementation of GZip. It’s a nifty compression tool that helps you compress files with blazing fast speeds. As an improvement of the good old gzip utility, it leverages multiple cores and processors to compress data.

This guide shines more light on Pigz and takes you through how to use the utility to compress files in Linux systems.

Installing Pigz on Linux Systems

Installing Pigz is a walk in the park because the Pigz package is contained in official repositories for major distributions such as Debian, and CentOS.

You can install Pigz in a single command in various distributions using their respective package managers as follows.

$ sudo apt install pigz  [On Debian/Ubuntu]
$ sudo dnf install pigz  [On CentOS/RHEL/Fedora]
$ sudo pacman -S pigz    [On Arch/Manjaro Linux] 
$ yay -S pigz

How to Compress Files with Pigz

To compress a single file to a zip format use the syntax.

$ pigz filename

In this guide, we will use the file ubuntu-20.04-beta-desktop-amd64.iso for demonstration purposes. To compress the file execute:

$ pigz ubuntu-20.04-beta-desktop-amd64.iso
Compress File in Linux
Compress File in Linux

However, the command deletes the original file upon compression as you might have noticed. To retain the original file after compression, run use the -k option as shown.

$ pigz -k ubuntu-20.04-beta-desktop-amd64.iso
Compress File without Delete
Compress File without Delete

From the output, we can clearly see that the original file has been retained even after compression.

Check Content of Compressed File in Linux

To check the contents of the compressed file, including the statistics on the compression ratio achieved use the -l option with pigz command:

$ pigz -l ubuntu-20.04-beta-desktop-amd64.iso.gz
View Content of Compressed File in Linux
View Content of Compressed File in Linux

From the output, you not only get to see the contents of the zipped file but also the percentage of compression which in this case is 1.9%.

Additionally, you can use various compression levels that exist from 1 to 9. The following compression levels are supported:

  • 6 – Default compression.
  • 1 – Fastest but offers the least compression.
  • 9 – Slowest but the best compression.
  • 0 – No compression.

For example, to compress the file with the best compression level, execute:

$ pigz -9 ubuntu-20.04-beta-desktop-amd64.iso

How to Compress a Directory with Pigz

By itself, Pigz does not have options to compress a folder, it only compresses single files. As a workaround, pigz is used in conjunction with tar command to zip directories.

To compress a directory, use the --use-compress-program argument as shown:

$ tar --use-compress-program="pigz -k " -cf dir1.tar.gz dir1
Compress a Directory in Linux
Compress a Directory in Linux

How to Limit the Number of Processors While Compressing

We mentioned earlier that the pigz utility tool uses multiple cores & processors when compressing files. You can specify the number of cores to be used using the -p option.

In this example, below, we have used the best compression (denoted by -9) with 4 processors (-p4) while retaining the original file (-k).

$ pigz -9 -k -p4 ubuntu-20.04-beta-desktop-amd64.iso

How to Decompress Files using Pigz

To decompress a file or directory using pigz, use the -d option or the unpigz command.

Using our compressed ISO file, the command will be:

$ pigz -d ubuntu-20.04-beta-desktop-amd64.iso
$ unpigz dir1.tar.gz
Decompress Files in Linux
Decompress Files in Linux

Comparison between Pigz vs Gzip

We went a bit further and pitted Pigz against Gzip tool.

Here are the results:

Gzip Compression

$ time gzip ubuntu-20.04-beta-desktop-amd64.iso
Check Gzip Compression Time
Check Gzip Compression Time

Pigz Compression

$ time pigz ubuntu-20.04-beta-desktop-amd64.iso
Check Pigz Compression Time
Check Pigz Compression Time

Gzip Decompression

$ time gzip -d ubuntu-20.04-beta-desktop-amd64.iso.gz
Check Gzip Decompression Time
Check Gzip Decompression Time

Pigz Decompression

$ time unpigz ubuntu-20.04-beta-desktop-amd64.iso.gz
Check Pigz Decompression Time
Check Pigz Decompression Time

From the comparison, we can clearly see that compression and decompression times for Pigz are much shorter than Gzip. This implies that the Pigz command-line tool is much faster than the Gzip tool

For more details on the usage of pigz command, visit the man pages.

$ man pigz

Furthermore, run the command below to view all the options available for use with pigz command.

$ pigz --help
Pigz Command Help
Pigz Command Help

And there you have it. We have covered the pigz command-line tool and showed you how you can compress and decompress files. We went further and compared Pigz with Gzip and found out that Pigz is the better of the two in terms of speed of both compression and decompression. We invite you to give it a shot and tell us how it went.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

James Kiarie
This is James, a certified Linux administrator and a tech enthusiast who loves keeping in touch with emerging trends in the tech world. When I'm not running commands on the terminal, I'm taking listening to some cool music. taking a casual stroll or watching a nice movie.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.


Leave a Reply
    • @Oejhal,

      After compressing a directory, it becomes a file, so you can use the same following command to decompress a file or directory using pigz, use the -d option, or the unpigz command.

      $ pigz -d ubuntu-20.04-beta-desktop-amd64.iso
      $ unpigz dir1.tar.gz
  1. It’s not a walk in the walk, it’s a walk in the park.

    There are a couple of things I wish you had tried:
    1) compressing a file with pigs and decompressing the file with gzip, and go the other way as well.
    2) Try alias gzip="pigz". Does that work?

    • Try alias gzip="pigz".

      The alias works but isn’t it confusing? What happens when actually you want to use ‘GZip’?


Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.