Search code examples
shellunixsolaris

execute shell with find gun at background


i have set of shells at the same directory ,I want to execute all shells that have result at the beginning like:

result.sha
result.shb
result.shc
...

the below script its worked find to execute all shells with matched reg, but how can I make each of them running at the background in parallel

find . -type f -name 'result.*' -exec sh {} \;

i have tried this but its not working:

find . -type f -name 'result.*' -exec sh {} \;&

Solution

  • More efficient to only start sh once per batch of find results, and let it fork off as many subprocesses as it wants.

    find . -type f -name 'result.*' -exec sh -c 'for arg do . "$arg" & done' _ {} +
    

    . "$arg" & forks off a copy of the already-running interpreter and runs the code in "$arg" inside it, avoiding paying interpreter startup costs extra times.