Bash 如何在目录中名为test1…test10的大约10个文件上并行运行md5sum

Bash 如何在目录中名为test1…test10的大约10个文件上并行运行md5sum,bash,shell,Bash,Shell,我可以串行运行,但无法并行运行。我的代码如下: #!/bin/bash while : do for i in `find ~/Mainstore-1/ -maxdepth 1 -type f` do md5sum $i done sleep 1 done 这应该做到: find ~/Mainstore-1/ -maxdepth 1 -type f -print0 | while read -d '' -r file ; do # launch md5sum i

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

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

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

如果文件名中有空格,您认为会发生什么情况?并引用你的变量!
while
循环的目的是什么?您希望这些文件会随着时间的推移而更改,还是会添加新文件?我希望新文件会添加到该目录中。不过,请注意名称中有换行符的文件。@Biffen是的,不错的旧换行符!:)我想知道是否可以使用find的exec选项实现它。
find~/Mainstore-1/-maxdepth 1-type f-I filename-exec bash-c“md5sum{}&”
@anishsane是的,或者更简单的
-exec md5sum{}+
注意
+
。但是,这不会并行处理校验和。(至少我怀疑md5sum是以这种方式实现的,但我不确定)
IFS=''
-print0
-d''
是另一种方式。但在本例中,
-exec
更具美感。