Even if it’s one of the faster archiving/compression solutions available, gzip has a tiny problem: it doesn’t support multiple processors/cores. Thus, if you have a relatively new PC, it doesn’t take advantage of its capabilities. Pigz is a multi-threaded implementation of gzip that allows you to compress files to the GNU zip format at a fraction of the time. Here is how to compress your files faster with pigz.
Installation
To install pigz on Ubuntu, Mint, and other Debian-compatible distributions, use:
sudo apt install pigz
On Arch and Manjaro Linux, install it with:
sudo pacman -S pigz
If you’re using openSUSE, try:
sudo zypper install pigz
Compress a Single File
To compress any file to GNU Zip format with pigz, use:
pigz NAME_OF_FILE
For some, it may be a problem that, by default, pigz deletes the original file after the compression. If you want to keep it, you must use the -k
switch (notice that it’s lowercase).
pigz -k NAME_OF_FILE
Pigz supports multiple levels of compression, and you can choose between them by stating their number after a dash. For example:
pigz -5 FILE_TO_BE_COMPRESSED
You can use numbers from 1 to 9, with “1” offering the quickest performance but lowest compression and “9” offering the slowest but highest compression.
Compressing Folders
Pigz has a significant restriction: it doesn’t support folders. You can only compress single files with it. A workaround is to use it together with tar.
Let’s say you want to compress your “Pictures” folder. Since tar supports the use of external compression programs, you can do the following:
tar --use-compress-program="pigz -k -9" -cf pictures.tar.gz Pictures
In the above command, tar --use-compress-program
states that although you will create a file archive with tar, the compression of its contents will happen through an external program.
This external program and its parameters are defined with the pigz -k -9
part of the command.
Finally, state that you want to Create a File (“-cf”) called “pictures.tar.gz” with everything found in the “Pictures” folder.
Decompressing Files And Folders
Decompressing any gz file with pigz is as simple as entering any of the following commands:
pigz -d FILENAME.gz unpigz FILENAME.gz
In our previously created tar.gz files, decompressing folders uses the same “tar” approach.
tar --use-compress-program="pigz -d" -xvf pictures.tar.gz
Limiting Parallelization
Finally, it should be noted that an extra switch might come in handy: p
. Pigz, by default, uses all processors/cores in your computer. When compressing large data sets, this can affect your computer’s responsiveness.
With the p
switch, you can limit pigz to only use a specific number of processors/cores. This will leave the rest of the cores free for your other tasks and interactivity. To do so, add the number of processors/cores right after the switch:
pigz -k -p2 FILE_TO_BE_COMPRESSED
-p2
restricts pigz to only use two processors/cores. You can use any number you like, but it’s suggested that you keep it within your hardware’s limits.
To learn more about file compression and extraction in Linux, check out our in-depth guide to Linux’s archiving and compression commands.
Image credit: Miguel Á. Padriñán @Pexels
Our latest tutorials delivered straight to your inbox