如何跨服务器拷贝指定文件

2015 年 9 月 28 日
 reticentfat
我想把所有 sh 文件从 a 服务器考到 b 服务器
locate *.sh 可以找到这些文件
如何把这些文件传到 scp 当参数
5583 次点击
所在节点    Linux
11 条回复
jarlyyn
2015 年 9 月 28 日
man find

大概是 find -name '*.sh' -exec scp {} xxxx@xxxx:/xxxx \;
zuo
2015 年 9 月 28 日
xargs 这个命令应该好用
iwannaflytomoon
2015 年 9 月 28 日
1) rsync --exclude-from=FILE --include-form=FILE
2) locate *.sh |xargs -I {} scp {} xxx@xxx://xxxx
reticentfat
2015 年 9 月 28 日
locate *.sh | xargs -i scp {} xxxx@xxxx:/home/gateway/sh_bak
find / -name '*.sh' -exec scp {} xxxx@xxxx:/xxxx \;
都可以实现,不过为啥每个文件我都需要输入一遍服务器密码呢
iwannaflytomoon
2015 年 9 月 28 日
scp 想不输入密码,需要生成密钥来 ssh 验证
wizardoz
2015 年 9 月 28 日
for e in `locate *.sh`
do
scp $e user@host_b:/your_target_dir/
done
sparkssssssss
2015 年 9 月 28 日
@reticentfat
先 find / -name *.sh |xargs tar czf sh.tgz 然后 scp
msg7086
2015 年 9 月 28 日
先搞起 ssh 证书认证,早日放弃密码连接。
zhangv
2015 年 9 月 28 日
scp
ceyes
2015 年 9 月 28 日
```

locate '*.sh' | xargs -i rsync -azh --progress {} user@server:/your/dir/

```

0. 建议先 ssh-copy-id ,免去输入密码。
1. xargs -i 把管道前的结果交给后面的程序,放到了{}的位置
2. rsync 用来同步文件,个人喜欢这个命令,当然 scp 也行。

rsync 的 -z 是压缩,-h 是 human-readable, --progres 是显示进度,不需要可以去掉。
rsync 有个 --dry-run ,用以不真的执行,方便测试下先。
quietin
2015 年 9 月 28 日
python 的 pexpect 可以解决输密码问题

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://v2ex.ih06.com/t/224187

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX