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.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Shervan
  • 287
  • 2
  • 4
  • 13

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.

roaima
  • 107,089
  • 14
  • 139
  • 261
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,454
  • 14
  • 63
  • 101
  • 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 09:15
  • [don't parse ls](http://mywiki.wooledge.org/ParsingLs): `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