1

I'm able to run serially but unable to run it parallely. My code is as below:

#!/bin/bash
while :
do
  for i in `find ~/Mainstore-1/ -maxdepth 1 -type f`
  do
    md5sum $i
  done
  sleep 1
done
anishsane
  • 20,270
  • 5
  • 40
  • 73
  • [Don't iterate the results of `find` like that!](http://stackoverflow.com/q/21663090/418066) What do you think will happen if there's a file with whitespace in its name? And quote your variables! – Biffen May 07 '15 at 06:55
  • 1
    What's the purpose of the `while` loop? Are you expecting the files to be changed over time, or new ones to be added? – anishsane May 07 '15 at 06:56
  • i'm expecting that new files will get added to that directory – Umashankar P M May 08 '15 at 08:56

1 Answers1

2

This should do:

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
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • 1
    Watch out for files with newlines in their names, though. – Biffen May 07 '15 at 07:04
  • @Biffen Yeah for sure, good old new lines! :) I was wondering if it can be achieved using find's exec option.. – hek2mgl May 07 '15 at 07:17
  • `find ~/Mainstore-1/ -maxdepth 1 -type f -I filename -exec bash -c "md5sum {} &" \;` – anishsane May 07 '15 at 07:21
  • 1
    @anishsane Yeah, or even simpler `-exec md5sum {} +` Note the `+`. However, this would not process the checksums in parallel. (At least I doubt that md5sum is implemented this way, however I'm not sure) – hek2mgl May 07 '15 at 07:22
  • 1
    `IFS=''`, `-print0` and `-d ''` is another way to go. But in this case `-exec` has a bit more beauty to it. – Biffen May 07 '15 at 07:27