我有数以千计的 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/57312894/