加入收藏 | 设为首页 | 会员中心 | 我要投稿 宁德站长网 (https://www.0593zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Windows > 正文

如何在Linux上复制文件/文件夹到远程系统?

发布时间:2019-03-02 14:55:02 所属栏目:Windows 来源:Prakash Subramanian
导读:副标题#e# 从一个服务器复制文件到另一个服务器,或者从本地到远程复制是 Linux 管理员的日常任务之一。 我觉得不会有人不同意,因为无论在哪里这都是你的日常操作之一。有很多办法都能处理这个任务,我们试着加以概括。你可以挑一个喜欢的方法。当然,看看

使用下面的 rsync 命令复制多个文件到远程服务器。

  1. # rsync -avz /home/daygeek/2g/shell-script/output.txt passwd-up.sh root@2g.CentOS.com:/opt/backup
  2.  
  3. sending incremental file list
  4. output.txt
  5. passwd-up.sh
  6.  
  7. sent 737 bytes received 50 bytes 1574.00 bytes/sec
  8. total size is 2537 speedup is 3.22

使用下面的 rsync 命令通过 ssh 复制单个文件到远程服务器。

  1. # rsync -avzhe ssh /home/daygeek/2g/shell-script/output.txt root@2g.CentOS.com:/opt/backup
  2.  
  3. sending incremental file list
  4. output.txt
  5.  
  6. sent 598 bytes received 31 bytes 419.33 bytes/sec
  7. total size is 2.47K speedup is 3.92

使用下面的 rsync 命令通过 ssh 递归地复制文件夹到远程服务器。这种方式只复制文件不包括文件夹。

  1. # rsync -avzhe ssh /home/daygeek/2g/shell-script/ root@2g.CentOS.com:/opt/backup
  2.  
  3. sending incremental file list
  4. ./
  5. output.txt
  6. ovh.sh
  7. passwd-up.sh
  8. passwd-up1.sh
  9. server-list.txt
  10.  
  11. sent 3.85K bytes received 281 bytes 8.26K bytes/sec
  12. total size is 9.12K speedup is 2.21

方式 5:如何在 Linux 上使用 rsync 命令和 Shell 脚本复制文件/文件夹到多个远程系统上?

如果你想复制同一个文件到多个远程服务器上,那也需要创建一个如下面那样的小 shell 脚本。

  1. # file-copy.sh
  2.  
  3. #!/bin/sh
  4. for server in `more server-list.txt`
  5. do
  6. rsync -avzhe ssh /home/daygeek/2g/shell-script/ root@2g.CentOS.com$server:/opt/backup
  7. done

上面脚本的输出。

  1. # ./file-copy.sh
  2.  
  3. sending incremental file list
  4. ./
  5. output.txt
  6. ovh.sh
  7. passwd-up.sh
  8. passwd-up1.sh
  9. server-list.txt
  10.  
  11. sent 3.86K bytes received 281 bytes 8.28K bytes/sec
  12. total size is 9.13K speedup is 2.21
  13.  
  14. sending incremental file list
  15. ./
  16. output.txt
  17. ovh.sh
  18. passwd-up.sh
  19. passwd-up1.sh
  20. server-list.txt
  21.  
  22. sent 3.86K bytes received 281 bytes 2.76K bytes/sec
  23. total size is 9.13K speedup is 2.21

方式 6:如何在 Linux 上使用 scp 命令和 Shell 脚本从本地系统向多个远程系统复制文件/文件夹?

在上面两个 shell 脚本中,我们需要事先指定好文件和文件夹的路径,这儿我做了些小修改,让脚本可以接收文件或文件夹作为输入参数。当你每天需要多次执行复制时,这将会非常有用。

  1. # file-copy.sh
  2.  
  3. #!/bin/sh
  4. for server in `more server-list.txt`
  5. do
  6. scp -r $1 root@2g.CentOS.com$server:/opt/backup
  7. done

输入文件名并运行脚本。

  1. # ./file-copy.sh output1.txt
  2.  
  3. output1.txt 100% 3558 3.5KB/s 00:00
  4. output1.txt 100% 3558 3.5KB/s 00:00

方式 7:如何在 Linux 系统上用非标准端口复制文件/文件夹到远程系统?

如果你想使用非标准端口,使用下面的 shell 脚本复制文件或文件夹。

(编辑:宁德站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!