0

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.

Shervan
  • 287

2 Answers2

4

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.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
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.

SHW
  • 14,786
  • 14
  • 66
  • 101