【问题标题】:How to run md5sum parallely on some 10 files named test1...test10 in the directory如何在目录中名为 test1...test10 的 10 个文件上并行运行 md5sum
【发布时间】:2015-05-07 06:52:38
【问题描述】:

我可以串行运行,但不能并行运行。我的代码如下:

#!/bin/bash
while :
do
  for i in `find ~/Mainstore-1/ -maxdepth 1 -type f`
  do
    md5sum $i
  done
  sleep 1
done

【问题讨论】:

  • Don't iterate the results of find like that! 如果文件名中包含空格,您认为会发生什么?并引用你的变量!
  • while 循环的目的是什么?您是否希望文件会随着时间的推移而更改,或者会添加新文件?
  • 我希望新文件会添加到该目录中

标签: bash shell


【解决方案1】:

应该这样做:

find ~/Mainstore-1/ -maxdepth 1 -type f -print0 | while read -d '' -r file ; do
    # launch md5sum in background using `&`
    md5sum "$file" &
done

# Wait for workers to finish
wait

【讨论】:

  • 不过,请注意名称中带有换行符的文件。
  • @Biffen 是的,当然,好旧的新线! :) 我想知道是否可以使用 find 的 exec 选项来实现..
  • find ~/Mainstore-1/ -maxdepth 1 -type f -I filename -exec bash -c "md5sum {} &" \;
  • @anishsane 是的,或者更简单的-exec md5sum {} + 请注意+。但是,这不会并行处理校验和。 (至少我怀疑 md5sum 是这样实现的,但是我不确定)
  • IFS=''-print0-d '' 是另一种方法。但在这种情况下,-exec 有更多的美感。
猜你喜欢
  • 2016-02-17
  • 2020-01-16
  • 1970-01-01
  • 2013-09-22
  • 2013-01-20
  • 2020-11-22
  • 2015-02-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多