0

This question already has an answer here:

I need to run same command separately for each file at the same time.

I've tried

for file in *.txt; do ./script <"$file"; done

but It starts the first one and waits until it get finished then goes to the next one.

I need to start them all together.

marked as duplicate by Gilles 'SO- stop being evil' command-line Sep 21 '16 at 22:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3

If script doesn't require any input from the user, you could use the shell's job processing features to run it in the background

for file in *.txt; do ./script <"$file" & done

When you append & to a command it's run in the background. Look up job control in the man page for bash (or your preferred shell) for details.

3
ls *.txt > list
cat list | parallel './script {}' &
wait

First list out the txt files in list file. parallel command will run the script on each file_name in background. Last wait command will wait for all the background jobs to finish then you can proceed for further commands.

  • What about ls -d *.txt | parallel './script {}'? I'm pretty sure you don't need to put parallel in the background only to wait for it. – roaima Sep 21 '16 at 9:15
  • don't parse ls: printf "%s\n" *.txt | parallel './script {}' – glenn jackman Sep 21 '16 at 19:37
  • @glennjackman what's the difference in this situation between ls -d *.txt and printf "%s\n" *.txt? Both break on file names containing \n. Both depend on parallel to "do the right thing" with weird characters in the filenames. – roaima Sep 21 '16 at 20:05
  • You don't need ls or printf for this... parallel alone can do it. – don_crissti Sep 21 '16 at 22:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.