bash: 运行 同时在多个目录上执行脚本

bash: running script on multiple directories simultaneously

假设我有多个目录:nmg_1, nmg_2,..., nmg_5。 这些目录中的每一个都包含两个子目录:fol, unf。 在每个子目录中都有一个文本文件。

所以,我有这个脚本:run.sh,就是读取每个子目录下的文本文件的某一行,输出另一个文件。因此,我想知道,有没有办法并行运行脚本,即脚本一次在多个子目录中运行?

给定 run.sh 一次对一个文本文件执行所需的处理,以下命令将使 4 个并行 bash 会话保持活动状态:

find -name "*.txt" -print0 | xargs -n 1 -P 4 -0 -I {} bash run.sh {}

来自man xargs

--max-procs=max-procs, -P max-procs
      Run up to max-procs processes at a time; the default is 1.
      If max-procs is 0, xargs will run as many processes as possible at a time. 
      Use the -n option with -P; otherwise chances are that only one exec will be done.