Posted on
Software

parallel: Run multiple shell commands in parallel

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Harnessing the Power of Parallel Processing in Bash

In the world of computing, time is of the essence. Efficiently managing time when running scripts or commands can drastically improve productivity and performance. This is where parallel, a remarkable shell tool, comes into play, allowing you to run multiple shell commands in parallel, rather than executing them sequentially. In this blog post, we'll explore what parallel is, why you might want to use it, and how to install and utilize it across various Linux distributions.

What is GNU Parallel?

GNU Parallel is a command-line utility that helps users execute jobs in parallel using one or more computers. It's a potent tool for running a series of commands concurrently, speeding up processing time considerably. While similar in concept to xargs, GNU Parallel is often preferred for its ability to efficiently handle complex tasks and its user-friendly reporting options.

Why Use GNU Parallel?

If you frequently find yourself waiting for a batch of shell commands to execute sequentially, you might benefit from Parallel. Here are a few scenarios where parallel shines:

  • Data Processing: Simultaneously processing multiple files or datasets.

  • Backup: Running multiple backup processes at the same time.

  • Image Processing: Employing image manipulation commands on multiple files concurrently.

Installing GNU Parallel

The installation process of GNU Parallel varies depending on the Linux distribution. Below are instructions for some of the most popular package managers:

Using apt (Debian, Ubuntu, and derivatives):

For systems based on Debian or Ubuntu, you can install GNU Parallel by using the apt command:

sudo apt update
sudo apt install parallel

Using dnf (Fedora):

On Fedora, the dnf package manager is employed:

sudo dnf install parallel

Using zypper (openSUSE):

For openSUSE users, parallel can be installed using zypper:

sudo zypper install parallel

How to Use GNU Parallel

After installing parallel, you can start using it to improve the efficiency of your shell scripts. Here’s a basic example to get you started.

Scenario: You have several text files in a directory and you want to count the number of lines in each file simultaneously.

Here’s how you would do it using parallel:

ls *.txt | parallel wc -l

This command uses ls to list files ending with .txt, and parallel to run wc -l on each file. The output will show the line counts for all files, processed in parallel.

Advanced Tips

  1. Job Control: Limit the number of jobs run at once by using -j:

    parallel -j 4 wc -l ::: *.txt
    

    This runs 4 jobs in parallel.

  2. Keeping Output Order: To keep the output in the same order as the input, use --keep-order or -k:

    parallel --keep-order wc -l ::: *.txt
    
  3. Progress Display: To see the progress, use --progress:

    parallel --progress wc -l ::: *.pdf
    

By harnessing the power of GNU Parallel, you can optimise your command execution times significantly, thereby improving workflow and productivity. Whether you’re handling data-intensive jobs, conducting multiple server requests, or simply running everyday scripts, Parallel can help you do more in less time. Give it a try and watch your efficiency soar!