Running N finds in parallel. Bash

One of the most beautiful things about bash is that you can actually create any number of subshells and make them run in parallel. I needed to perform a very large find (believe me, it is huge, it’s huge goey) that would take “billions and billions” of years to finish. Thanks to subshells it finished way quicker.

The code inside ( ) will be executed in a subshell, if you put a & after that, it will run in the background. A ‘wait’ should be placed before the end to wait for the background processes to finish.

Code goes as follows:

#!/bin/bash

for i in `ls | grep folder_regex`
do
    # One process per folder
    (
        [ -d "$i/subfolder" ] || continue
        echo "Searching in $i/subfolder"
        find ./$i/subfolder/ -iname "regex" >> ~/results/$i"_results.lst"
    ) &
done

wait

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s