如何编写 scp -r copy 脚本?
我似乎无法说服 scp 遵守规矩。
对于测试数据
ubuntu@domU-12-31-38-00-D4-F1:/tmp$ find /tmp/a1/
/tmp/a1/
/tmp/a1/a2
/tmp/a1/a2/a3
在发出命令时,
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
我希望在 domU-12-31-38-00-E2-52.compute-1.internal 上创建相同的目录结构,无论目标主机上是否存在目录 /tmp/a1 。相反,scp 实际创建的是以下结构(如果目标主机上存在 /tmp/a1)
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
如何强制 scp 复制到给定目录作为操作的根目录?
稍后我想编写此操作的脚本,以便主服务器上的给定目录路径可以调用一个脚本,该脚本将相同的目录结构复制到所有从服务器。请注意,rsync 行为(至少从我测试过的情况来看)在这种方式下是相同的。
谢谢你, 格言。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
而不是使用
$ scp -r /tmp/a1
使用这个:
$ scp -r /tmp/a1/*
这将复制 /tmp/a1 下的每个文件,因此目标的结果结构将是
/tmp/a1/a2/a3
否则,我建议将目标目录简单地设置为“/tmp/”,而不是“/tmp/a1/”,无论哪种方式都可以。关于 rsync,不,它不一样:
**this/**
到 rsync 意味着**this/**
就scp或<代码>cp。尾部斜杠
[this/]
表示复制文件夹的内容。如果您将尾部斜杠关闭,它将复制目录本身[如果是递归的]。希望您发现这很有用。 :)
马特
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 ofscp
orcp
. 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
scp
模仿cp
的行为,即复制到目标目录(如果存在)。只需将其复制到/tmp
而不是/tmp/a1
中。scp
is mimicking the behavior ofcp
, which is to copy INTO the target directory (if it exists). Just have it copy into/tmp
rather than/tmp/a1
.