V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
reticentfat
V2EX  ›  Linux

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

  •  
  •   reticentfat · 2015-09-28 11:07:51 +08:00 · 4135 次点击
    这是一个创建于 3104 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我想把所有 sh 文件从 a 服务器考到 b 服务器
    locate *.sh 可以找到这些文件
    如何把这些文件传到 scp 当参数
    11 条回复    2015-09-28 22:10:24 +08:00
    jarlyyn
        1
    jarlyyn  
       2015-09-28 11:27:49 +08:00
    man find

    大概是 find -name '*.sh' -exec scp {} xxxx@xxxx:/xxxx \;
    zuo
        2
    zuo  
       2015-09-28 12:06:24 +08:00
    xargs 这个命令应该好用
    iwannaflytomoon
        3
    iwannaflytomoon  
       2015-09-28 12:21:22 +08:00
    1) rsync --exclude-from=FILE --include-form=FILE
    2) locate *.sh |xargs -I {} scp {} xxx@xxx://xxxx
    reticentfat
        4
    reticentfat  
    OP
       2015-09-28 12:40:21 +08:00
    locate *.sh | xargs -i scp {} xxxx@xxxx:/home/gateway/sh_bak
    find / -name '*.sh' -exec scp {} xxxx@xxxx:/xxxx \;
    都可以实现,不过为啥每个文件我都需要输入一遍服务器密码呢
    iwannaflytomoon
        5
    iwannaflytomoon  
       2015-09-28 12:43:56 +08:00
    scp 想不输入密码,需要生成密钥来 ssh 验证
    wizardoz
        6
    wizardoz  
       2015-09-28 12:59:45 +08:00   ❤️ 1
    for e in `locate *.sh`
    do
    scp $e user@host_b:/your_target_dir/
    done
    coolloves
        7
    coolloves  
       2015-09-28 13:30:37 +08:00
    @reticentfat
    先 find / -name *.sh |xargs tar czf sh.tgz 然后 scp
    msg7086
        8
    msg7086  
       2015-09-28 13:40:08 +08:00
    先搞起 ssh 证书认证,早日放弃密码连接。
    zhangv
        9
    zhangv  
       2015-09-28 13:42:25 +08:00
    scp
    ceyes
        10
    ceyes  
       2015-09-28 14:25:28 +08:00   ❤️ 1
    ```

    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
        11
    quietin  
       2015-09-28 22:10:24 +08:00
    python 的 pexpect 可以解决输密码问题
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1007 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 19:29 · PVG 03:29 · LAX 12:29 · JFK 15:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.