Clicky

How to Use Parallel gzip and bzip2 on macOS

ProductivityAugust 25, 2023
black and silver hard disk drive

Introduction

In the realm of data compression, gzip and bzip2 are two stalwarts that have served us well. But as we move into an era where multi-core processors are the norm rather than the exception, it’s time to harness their power for our compression tasks. This article will guide you through installing and using parallel versions of gzip and bzip2 on macOS, leveraging Homebrew, a popular package manager. We’ll also explore how to make these tools your default choice for compression tasks and whether any additional steps are needed to utilize multiple cores effectively.

Installing Parallel gzip and bzip2 using Homebrew

Homebrew, the beloved package manager for macOS, makes it a breeze to install software not bundled with macOS. If you haven’t installed Homebrew yet, you can do so by pasting the following command in your terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Once Homebrew is ready to serve, installing parallel versions of gzip and bzip2—namely pigz and pbzip2—is as simple as running these commands:

brew install pigz
brew install pbzip2

These commands will fetch the latest versions of pigz (parallel gzip) and pbzip2 (parallel bzip2), compile them, and place them in your system’s PATH. Now that we have our tools installed let’s move on to making them our default choice for compression tasks.

Please note: While Homebrew handles most dependencies automatically, there might be cases where additional libraries or tools are required. In such scenarios, Homebrew will provide instructions on how to proceed.

Setting Parallel gzip and bzip2 as Default

Now that we have parallel versions of gzip and bzip2 installed, it’s time to make them our default choice for compression tasks. This can be achieved by creating aliases in your shell configuration file.

If you’re using bash or zsh (the default shell on macOS Catalina and later), open your .bashrc or .zshrc file with a text editor and add the following lines at the end of the file:

alias gzip='pigz'
alias bzip2='pbzip2'

Save and close the file. To apply these changes immediately, source your configuration file with one of these commands:

source ~/.bashrc  # For bash users
source ~/.zshrc   # For zsh users

From now on, whenever you use gzip or bzip2, your system will use pigz and pbzip2 instead, taking advantage of multiple cores for faster compression.

Utilizing Multiple Cores

One of the key advantages of using pigz and pbzip2 is their ability to utilize multiple cores for compression tasks. But do we need to do anything extra to enable this feature? The answer is no.

Both pigz and pbzip2 are designed to automatically use all available processor cores for compression tasks. This means you don’t have to fiddle with any settings or flags to take advantage of your multi-core processor. Simply run your compression commands as usual, and these tools will handle the rest.

However, if you wish to limit the number of cores used by these tools (for instance, if you’re running other resource-intensive tasks), you can do so using the -p flag followed by the number of cores:

gzip -p 4 file.txt   # For pigz users
bzip2 -p 4 file.txt  # For pbzip2 users

In these examples, gzip and bzip2 will only use four cores for compressing ‘file.txt’.

Conclusion

In this digital age, where data is growing exponentially, efficient compression tools are more important than ever. By harnessing the power of multi-core processors with parallel versions of gzip and bzip2, we can significantly speed up our compression tasks. With Homebrew’s simplicity and the automatic multi-core utilization of pigz and pbzip2, it’s never been easier to upgrade your macOS compression toolkit.

Sources