Compress Files Faster with Pigz Tool in Linux

Posted by

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

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

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

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

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

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

Pigz Compression

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

Gzip Decompression

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

Pigz Decompression

$ time unpigz ubuntu-20.04-beta-desktop-amd64.iso.gz
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

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. Techexpert invite you to give it a shot and tell us how it went.

Leave a Reply

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