Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to copy files / folders to a remote system on Linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to copy files / folders to a remote system on Linux. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Copying files from one server to another, or locally to remotely, is one of the daily tasks of Linux administrators.

I don't think anyone will disagree, because wherever you are, it's one of your daily operations. There are many ways to deal with this task, and we try to summarize it. You can choose a method you like. Of course, looking at other orders can help you in other places.

I have tested all the commands and scripts in my own environment, so you can use them directly in your daily work.

Usually people prefer scp because it is one of the native commands native command for file copying. But the other commands listed in this article are also useful, so I suggest you give it a try.

File replication can be easily done in the following four ways.

Scp: copy files between two hosts on the network, it uses ssh for file transfer, and uses the same authentication method, with the same security.

Rsync: is a fast and outstanding multi-function file copy tool. It can replicate locally, between other hosts through a remote shell, or with a remote rsync

Daemon daemon

Copy between.

Pscp: a program that copies files to multiple hosts in parallel. It provides many features, such as configuring secret-free transfer for scp, saving output to files, and timeout control.

Prsync: also a program that copies files to multiple hosts in parallel. It also provides many features, such as configuring secret-free transfer for ssh, saving output to files, and timeout control.

Method 1: how do I use the scp command on Linux to copy files / folders from the local system to the remote system?

The scp command allows us to copy files / folders from the local system to the remote system.

I will copy the output.txt file from the local system to the / opt/backup folder on the 2g.CentOS.com remote system.

# scp output.txt root@2g.CentOS.com:/opt/backup output.txt 100% 2468 2.4KB/s 00:00

Copy two files output.txt and passwd-up.sh from the local system to the / opt/backup folder on the remote system 2g.CentOs.com.

# scp output.txt passwd-up.sh root@2g.CentOS.com:/opt/backup output.txt 100% 2468 2.4KB/s 00:00passwd-up.sh 100% 877 0.9KB/s 00:00

Copy the shell-script folder from the local system to the / opt/back folder of the remote system 2g.CentOs.com.

This will be copied to / opt/back along with all the files in the shell-script folder.

# scp-r / home/daygeek/2g/shell-script/ root@:/opt/backup/ output.txt 100% 2468 2.4KB/s 00:00ovh.sh 100% 76 0.1KB/s 00:00passwd-up.sh 100% 877 0.9KB/s 00:00passwd-up1.sh 100% 0.0KB/s 00:00server-list.txt 100% 23 0.0KB/s 00:00 method 2: how do I copy files / folders to multiple remote systems using the scp command and Shell script on Linux?

If you want to copy the same file to multiple remote servers, you need to create a small shell script like the one below.

Also, you need to add the server to the server-list.txt file. Ensure that after the addition is successful, each server should have a separate line.

In the end, the script you want looks like this:

# file-copy.sh #! / bin/shfor server in `more server- list.txt`do scp / home/daygeek/2g/shell-script/output.txt root@$server:/opt/backupdone

When you are finished, set executable permissions to the files in file-copy.sh.

# chmod + x file-copy.sh

Then run the script to complete the replication.

#. / file-copy.sh output.txt 100% 2468 2.4KB/s 00:00output.txt 2468 2.4KB/s 00:00

Use the following script to copy multiple files to multiple remote servers.

# file-copy.sh #! / bin/shfor server in `more server- list.txt`do scp / home/daygeek/2g/shell-script/output.txt passwd-up.sh root@$server:/opt/backupdone

The following results show that all two files are copied to both servers.

#. / file-cp.sh output.txt 100% 2468 2.4KB/s 00:00passwd-up.sh 100% 877 0.9KB/s 00:00output.txt 100% 2468 2.4KB/s 00:00passwd-up.sh 100% 877 0.9KB/s 00:00

Use the following script to recursively copy folders to multiple remote servers.

# file-copy.sh #! / bin/shfor server in `more server- list.txt`do scp-r / home/daygeek/2g/shell-script/ root@$server:/opt/backupdone

The output of the above script.

#. / file-cp.sh output.txt / 2468 2.4KB/s 00:00ovh.sh / 76 0.1KB/s 00:00passwd-up.sh / 877 0.9KB/s 00:00passwd-up1.sh / 7 0.0KB/s 00:00server-list.txt / 23 0.0KB/s 00:00 output.txt / 2468 2.4KB/s 00:00ovh.sh / 76 0.1KB/s 00:00passwd-up.sh / 877 0 .9KB / s 00:00passwd-up1.sh 100% 7 0.0KB/s 00:00server-list.txt 100% 23 0.0KB/s 00:00 mode 3: how do I use the pscp command on Linux to copy files / folders to multiple remote systems?

The pscp command allows us to copy files directly to multiple remote servers.

Use the following pscp command to copy a single file to the remote server.

# pscp.pssh-H 2g.CentOS.com / home/daygeek/2g/shell-script/output.txt / opt/backup [1] 18:46:11 [SUCCESS] 2g.CentOS.com

Use the following pscp command to copy multiple files to the remote server.

# pscp.pssh-H 2g.CentOS.com / home/daygeek/2g/shell-script/output.txt ovh.sh / opt/backup [1] 18:47:48 [SUCCESS] 2g.CentOS.com

Use the following pscp command to recursively copy the entire folder to the remote server.

# pscp.pssh-H 2g.CentOS.com-r / home/daygeek/2g/shell-script/ / opt/backup [1] 18:48:46 [SUCCESS] 2g.CentOS.com

Use the following pscp command to copy a single file to multiple remote servers.

# pscp.pssh-h server-list.txt / home/daygeek/2g/shell-script/output.txt / opt/backup [1] 18:49:48 [SUCCESS] 2g.CentOS.com [2] 18:49:48 [SUCCESS] 2g.Debian.com

Use the following pscp command to copy multiple files to multiple remote servers.

# pscp.pssh-h server-list.txt / home/daygeek/2g/shell-script/output.txt passwd-up.sh / opt/backup [1] 18:50:30 [SUCCESS] 2g.Debian.com [2] 18:50:30 [SUCCESS] 2g.CentOS.com

Use the following command to recursively copy folders to multiple remote servers.

# pscp.pssh-h server-list.txt-r / home/daygeek/2g/shell-script/ / opt/backup [1] 18:51:31 [SUCCESS] 2g.Debian.com [2] 18:51:31 [SUCCESS] 2g.CentOS.com mode 4: how to use the rsync command on Linux to copy files / folders to multiple remote systems?

Rsync is a fast and outstanding multi-function file copying tool. It can replicate locally, between other hosts via remote shell, or between remote rsync daemons, daemon.

Use the following rsync command to copy a single file to the remote server.

# rsync-avz / home/daygeek/2g/shell-script/output.txt root@:/opt/backup sending incremental file listoutput.txt sent 598 bytes received 31 bytes 1258.00 bytes/sectotal size is 2468 speedup is 3.92

Use the following rsync command to copy multiple files to the remote server.

# rsync-avz / home/daygeek/2g/shell-script/output.txt passwd-up.sh root@2g.CentOS.com:/opt/backup sending incremental file listoutput.txtpasswd-up.sh sent 737 bytes received 50 bytes 1574.00 bytes/sectotal size is 2537 speedup is 3.22

Use the following rsync command to copy a single file to the remote server through ssh.

# rsync-avzhe ssh / home/daygeek/2g/shell-script/output.txt root@2g.CentOS.com:/opt/backup sending incremental file listoutput.txt sent 598 bytes received 31 bytes 419.33 bytes/sectotal size is 2.47K speedup is 3.92

Use the following rsync command to recursively copy the folder to the remote server through ssh. In this way, only files are copied, not folders.

# rsync-avzhe ssh / home/daygeek/2g/shell-script/ root@2g.CentOS.com:/opt/backup sending incremental file list./output.txtovh.shpasswd-up.shpasswd-up1.shserver-list.txt sent 3.85K bytes received 281 bytes 8.26K bytes/sectotal size is 9.12K speedup is 2.21 method 5: how to copy files / folders to multiple remote systems using the rsync command and Shell script on Linux?

If you want to copy the same file to multiple remote servers, you also need to create a small shell script like the one below.

# file-copy.sh #! / bin/shfor server in `more server- list.txt`do rsync-avzhe ssh / home/daygeek/2g/shell-script/ root@2g.CentOS.com$server:/opt/backupdone

The output of the above script.

#. / file-copy.sh sending incremental file list./output.txtovh.shpasswd-up.shpasswd-up1.shserver-list.txt sent 3.86K bytes received 281 bytes 8.28K bytes/sectotal size is 9.13K speedup is 2.21 sending incremental file list./output.txtovh.shpasswd-up.shpasswd-up1.shserver-list.txt sent 3.86K bytes received 281 bytes 2.76K bytes/sectotal size is 9.13K speedup is 2.21 method 6: how to use the scp command and Shell foot on Linux Copy files / folders from the local system to multiple remote systems?

In the above two shell scripts, we need to specify the path of the file and folder in advance, and here I have made some minor changes so that the script can accept the file or folder as input parameters. This is useful when you need to perform copies multiple times a day.

# file-copy.sh #! / bin/shfor server in `more server- list.txt`doscp-r $1 root@2g.CentOS.com$server:/opt/backupdone

Enter a file name and run the script.

#. / file-copy.sh output1.txt output1.txt 3558 3.5KB/s 00:00output1.txt 3558 3.5KB/s 00:00 method 7: how do I copy files / folders to a remote system using a non-standard port on a Linux system?

If you want to use a non-standard port, use the shell script below to copy files or folders.

If you are using a non-standard Non-Standard port, make sure that the port number is specified as shown in the scp command below.

# file-copy-scp.sh #! / bin/shfor server in `more server- list.txt`doscp-P 2222-r $1 root@2g.CentOS.com$server:/opt/backupdone

Run the script and enter a file name.

#. / file-copy.sh ovh.sh ovh.sh 100% 3558 3.5KB/s 00:00ovh.sh 3558 3.5KB/s 00:00

If you are using a non-standard Non-Standard port, make sure that the port number is specified as shown in the rsync command below.

# file-copy-rsync.sh #! / bin/shfor server in `more server- list.txt`dorsync-avzhe 'ssh-p 2222' $1 root@2g.CentOS.com$server:/opt/backupdone

Run the script and enter a file name.

#. / file-copy-rsync.sh passwd-up.shsending incremental file listpasswd-up.sh sent 238 bytes received 35 bytes 26.00 bytes/sectotal size is 159 speedup is 0.58 sending incremental file listpasswd-up.sh sent 238 bytes received 35 bytes 26.00 bytes/sectotal size is 159 speedup is 0.58 this article on "how to copy files / folders on Linux to a remote system" ends here. I hope the above content can be of some help to you. So that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report