If the csv files are generated by the java command, this will fail because the mv will run before any files have been generated. Since all java processes are sent to the background, the loop will finish almost immediately, so the script continues to the mv which finds no files to move and so does nothing.
A simple solution is to use wait. From help wait (in a bash shell):
$ help wait
wait: wait [-fn] [-p var] [id ...]
Wait for job completion and return exit status.
Waits for each process identified by an ID, which may be a process ID or a
job specification, and reports its termination status. If ID is not
given, waits for all currently active child processes, and the return
status is zero. If ID is a job specification, waits for all processes
in that job's pipeline.
The relevant bit here is "If ID is not given, waits for all currently active child processes". This means that you can just add wait after your loop and that will make the script wait until all child processes are finished before continuing:
#!/bin/bash
for f in ./lqns/*.lqn
do
java -jar DiffLQN.jar "$f" &
done
wait
mv ./lqns/*.csv csvs
Alternatively, you can combine the java and mv commands:
#!/bin/bash
for f in ./lqns/*.lqn
do
java -jar DiffLQN.jar "$f" && mv /lqns/*.csv csvs &
done
Another, possibly better, option is to use GNU parallel (which should be in the repositories of whatever operating system you are running), a tool designed for precisely this sort of thing. With it, you could do:
parallel java -jar DiffLQN.jar ::: ./lqns/*.lqn
mv ./lqns/*.csv csvs
lqnsfolder. I wish the execution to reach themvline and then return but it simply stays on hold without moving the files in thecsvsfolder.javacommand? The thing is that yourmvwill run almost immediately: since all your processes are sent to the background, the loop will exit in milliseconds and themvwill be run immediately. So if the csv files are generated by the java commands, themvwon't find anything to copy. Is that what is happening?javacommand generates thecsvfiles but themvcommand doesn't do anything... So that is definitely the explanation. How do I ensure that themvcommand is run only after alljavacommands finished the execution?