bash - 在 bash 中进行并行处理?

标签 bash parallel-processing

我有数以千计的 png 文件,我想使用 pngcrush 将它们缩小。我有一个简单的 find .. -exec 作业,但它是顺序的。我的机器有很多资源,我会并行处理。

对每个png要执行的操作是:

pngcrush input output && mv output input

理想情况下,我可以指定并行操作的最大数量。

有没有办法用 bash 和/或其他 shell 助手来做到这一点?我是 Ubuntu 或 Debian。

最佳答案

您可以使用 xargs并行运行多个进程:

find /path -print0 | xargs -0 -n 1 -P <nr_procs> sh -c 'pngcrush $1 temp.$$ && mv temp.$$ $1' sh

xargs将读取由 find 生成的文件列表(由 0 个字符分隔(-0))并一次使用一个参数(sh -c '...' sh)运行提供的命令(-n 1)。 xargs 将运行 <nr_procs> ( -P <nr_procs> ) 并行。

关于bash - 在 bash 中进行并行处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3811943/

相关文章:

bash - 为什么将 `trap -` 放入函数中时不起作用?

linux - R - makeCluster 在 Linux 中卡在本地主机上

multithreading - 如何使 Julia 中的多线程与线程数成比例?

haskell - 使用 repa 时将 Identity monad 与 mmultP 一起使用有什么问题?

bash - 您如何转义您不想为 sed 评估的用户提供的搜索词?

bash - 端口监听使用/dev/tcp

regex - 如何使用 grep 和正则表达式从字符串中提取数字

linux - Bash 脚本在终端仿真器中工作,但不能作为 i3 键绑定(bind)

.net - 你在使用并行扩展吗?

java - Gradle,并行任务执行