-4

I want to copy directories from one variable to directories from another one but without loop.

from="fromdir1 fromdir2"
to="todir1 todir2"

I mean fromdir1 to todir1, fromdir2 to todir2.

I think it can be done with xargs but I don't know how.

trilisser
  • 1
  • 2
  • 1
    Hi. You've not completely stated what you want: do you want to copy **each** directory from `from` to **each** directory in `to`, or do you just want to copy the first entry of `from` to the first entry of `to`, the second entry of `from` to the second entry of `to` and so on? – Marcus Müller Jun 21 '23 at 08:14
  • Hello. Thanks for the correction. I edited the post. I meant `fromdir1` to `todir1` and `fromdir2` to `todir2` – trilisser Jun 21 '23 at 08:20
  • 1
    But then xargs is the wrong tool and you already have gotten a solution in a previous question of yours. You need a loop, and it goes like this: – Marcus Müller Jun 21 '23 at 08:26
  • my question was **without** loop – trilisser Jun 21 '23 at 08:57
  • yes, I know, but that can't work. `xargs` doesn't do what you say you *think* it does, so the answer you've already gotten is the best you can get. – Marcus Müller Jun 21 '23 at 09:04
  • yeah, I got it, but it was just an assumption, so the `parallel` solution below is appropriate – trilisser Jun 22 '23 at 07:43

1 Answers1

1

You could use GNU parallel with linked arguments:

parallel --link cp {1} {2} ::: from1 from2 from3 ::: to1 to2 to3

If the from and to-files are in respectives text lists, use

parallel --link cp {1} {2} :::: fromlist :::: tolist

note the 4 colons vs. 3 colons previously. More info on GNU parallel on the website.

For reading them from bash array variables, this will do:

parallel --link cp {1} {2} ::: "${from[@]}" ::: "${to[@]}"
FelixJN
  • 12,616
  • 2
  • 27
  • 48