简体   繁体   中英

How to script scp -r copy?

I can't seem to be able to convince scp to behave.

For test data

ubuntu@domU-12-31-38-00-D4-F1:/tmp$ find /tmp/a1/
/tmp/a1/
/tmp/a1/a2
/tmp/a1/a2/a3

On issuing the command

ubuntu@domU-12-31-38-00-D4-F1:/tmp$ scp -r /tmp/a1 domU-12-31-38-00-E2-52.compute-1.internal:/tmp/a1

I would expect the same directory structure created on domU-12-31-38-00-E2-52.compute-1.internal, whatever the directory /tmp/a1 exists on target host or not. Instead, what scp actually creates is the following structure (if /tmp/a1 exists on target host)

ubuntu@domU-12-31-38-00-D4-F1:/tmp$ ssh domU-12-31-38-00-E2-52.compute-1.internal find /tmp/a1 
/tmp/a1
/tmp/a1/a1
/tmp/a1/a1/a2
/tmp/a1/a1/a2/a3

How can scp be forced to copy into given directory as the root of the operation?

Later on I would like to script this operation so that given directory path on master I can call a script that will replicate the same directory structure to all slaves. Please note that rsync behavior (at least from what I've tested) is the same in this manner.

Thank you, Maxim.

scp is mimicking the behavior of cp , which is to copy INTO the target directory (if it exists). Just have it copy into /tmp rather than /tmp/a1 .

Instead of using $ scp -r /tmp/a1 use this: $ scp -r /tmp/a1/* that will copy every file under /tmp/a1, so your resulting structure of your destination will be /tmp/a1/a2/a3 Or else, I would suggest making the target directory simply "/tmp/", not "/tmp/a1/", either way will work. And about rsync, no, it is not the same: **this/** to rsync means **this/** in terms of scp or cp . The trailing slash [this/] means to copy the contents of the folder. If you leave the trailing slash off , it will copy the directory itself [if recursive].

Hope you've found this useful. :) Matt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM