0

I have a need to copy one file from /a/b/ folder to /a/c/, a/d/, a/e/

Besides using

cp file.name /a/c cp file.name /a/d cp file.name /a/e

in the terminal, is there a way to use one line? I have read and seen that thera are ways, but I think my situation is different as my destination folder names are different.

I also want to ask that is it possible to cp a file into a destination folder and one recursive level in. So if there is a folder that has a/b/c/d, and you drop it in a, it goes to b and b/c, and not b/c/d.

1
  • I understand the responses perspectives, so let me clarify. The environment is as such: One terminal is connecting to 5 different servers. Each server has the same file structure (they are copies of one another like nodes), but for the destination folders for the file. So I am using parallel-ssh to connect, so you can understand the perspective of using one command line, but can cater for all 5 different servers.
    – Niko
    Feb 28, 2018 at 5:14

2 Answers 2

0

Well that's bad because cp can copy multiple sources but cannot copy to multiple destinations which is what you need. For this to work you have invoke cp multiple times - once per destination which you have already tried. What you want can be accomplished by

1. Using for loop

for i in /a/c/ a/d/ a/e/; do cp "$file" "$i"; done

This is pretty mush self-explained.

2. combining cp with xargs :

$ echo dir1 dir2 dir3 | xargs -n 1 cp file1

Will copy file1 to dir1, dir2, and dir3. xargs will call cp 3 times to do this. xargs will invoke cp 3 times and copy file1 to dir1, dir2 and dir3 in successive calls.

In your case it will be

$ echo /a/c/ a/d/ a/e/ | xargs -n 1 cp /a/b/file

For further details about xargs, see manpage.

0

It's a bit of a cheat, but you could redirect the content of the file to multiple destinations using tee:

tee a/{b..d}/file.name < file.name > /dev/null
1
  • this would work if a/{wildcard}/file.name - as an example
    – Niko
    Feb 28, 2018 at 5:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .