Linux将目录和内容从远程复制到本地,反之亦然
在本教程中,我将介绍:
Unix和Linux使用
rsync remote to local server
复制目录和内容Unix和Linux使用
rsync to remote server
复制目录和内容。Unix和Linux使用scp从本地服务器复制目录和文件到远程服务器。
Unix和Linux使用scp从远程服务器复制目录和文件到本地服务器。
scp recursive
在Unix和Linux中复制目录和内容Unix和Linux将文件从ssh复制到本地服务器
Linux中有多种命令可用于将目录和内容从一台服务器复制到另一台Linux中。
在本文中,我将在Linux中使用从本地到远程服务器的scp,从远程到本地服务器的scp,从rsync远程到本地和rsync到远程服务器的共享在Linux复制目录和文件中的命令和参数。
将目录和文件从本地复制到远程服务器
我们可以使用scp或者rsync将文件夹和文件从本地复制到ssh或者将文件夹和文件从ssh复制到本地的相同或者不同目录中。
默认情况下,复制文件和文件夹是"顺序"进行的。
如果要并行复制目录和内容,则必须使用pscp或者pssh工具。我们还可以配置从本地到远程或者从远程到本地的无密码复制,因此我们不必每次尝试在服务器之间复制文件和文件夹时都提供密码。
这对于自动化非常有用。
1. Linux使用scp递归复制目录和文件
scp是一种安全的远程复制工具,用于在多个Linux服务器之间复制目录和内容。
若要仅将文件从本地服务器复制到远程服务器,则不需要带scp的任何其他参数。
但是要复制目录和内容,我们需要使用
-r
参数进行scp递归如果使用不带-r的scp,则该工具只能将文件(而不是目录)从本地复制到远程服务器,反之亦然。
1.1:使用scp从本地到远程服务器保持"相同"目录名称
在这种scp语法中,将目录及其内容复制到远程服务器后,我们将保留相同的目录名称
scp语法:
scp -r <path to dir on localhost> user<@host>:<path on remote host>/
让我将目录"/tmp/hynman"中的所有文件从本地复制到远程服务器上"/home/temp /"下的远程服务器。
以下是我的本地主机上的/tmp/hynman目录内容,因此我将使用scp递归将目录/tmp/hynman中的所有文件复制到远程服务器。
server1:~ # ls -l /tmp/hynman/ total 8 -rw-r----- 1 root root 0 Jan 10 16:38 file1 -rw-r----- 1 root root 0 Jan 10 16:38 file2 -rw-r----- 1 root root 0 Jan 10 16:38 file3 drwxr-x--- 2 root root 4096 Jan 10 16:39 test1 drwxr-x--- 2 root root 4096 Jan 10 16:39 test2
因此,接下来使用scp递归,我将目录和内容从本地复制到远程Linux服务器
在此示例中,本地和远程服务器上的"目录名将相同",因为我们在本地主机上使用scp给出目录名后没有给出" /"(突出显示)。
server1:~ # scp -r /tmp/hynman temp@server2:/home/temp/ Password: file1 100% 0 0.0KB/s 00:00 file3 100% 0 0.0KB/s 00:00 file2 100% 0 0.0KB/s 00:00
从本地到远程的ssh复制文件成功。
接下来验证远程服务器上的传输
server2:~ # ll /home/temp/hynman/ total 8 drwxr-x--- 2 temp users 4096 Jan 10 16:50 test1 -rw-r----- 1 temp users 0 Jan 10 16:50 file1 drwxr-x--- 2 temp users 4096 Jan 10 16:50 test2 -rw-r----- 1 temp users 0 Jan 10 16:50 file3 -rw-r----- 1 temp users 0 Jan 10 16:50 file2
1.2:使用scp将目录名从本地更改为远程服务器
如果我们希望使用"不同目录名"将目录/tmp/hynman复制到远程服务器,请使用以下语法
scp语法:
scp -r <path to dir on localhost>/* user<@host>:<path on remote host>/
其中如果我们遵循scp
语法,我在本地主机目录名的末尾提供了/*。
因此,这里我们将所有文件复制到目录"/tmp/hynman"中,并将其存储在远程服务器上的/home/temp/rahul /下
因此,我们会看到对于从本地服务器到远程服务器的scp来说,此正斜杠(//)非常重要。
以以下格式执行命令
server1:~ # scp -r /tmp/hynman/* temp@server2:/home/temp/rahul/ Password: file1 100% 0 0.0KB/s 00:00 file2 100% 0 0.0KB/s 00:00 file3 100% 0 0.0KB/s 00:00
目录"/tmp/hynman"下的所有文件已成功复制到远程服务器。
验证" server2"节点上的内容。
server2:~ # ls -l /home/temp/rahul/ total 8 -rw-r----- 1 temp users 0 Jan 10 16:54 file1 -rw-r----- 1 temp users 0 Jan 10 16:54 file2 -rw-r----- 1 temp users 0 Jan 10 16:54 file3 drwxr-x--- 2 temp users 4096 Jan 10 16:54 test1 drwxr-x--- 2 temp users 4096 Jan 10 16:54 test2
2.使用rsync将文件夹和文件从本地复制到远程服务器
rsync是在Linux和Unix中将目录和内容从本地复制到远程服务器的另一种更好的选择。
它以其增量传输算法而闻名,该算法通过仅发送源文件和目标中现有文件之间的差异来减少通过网络发送的数据量。
Rsync被广泛用于备份和镜像,并作为日常使用的改进复制命令
Rsync查找需要使用"快速检查"算法(默认情况下)传输的文件,该算法查找大小或者上次修改时间已更改的文件。
2.1:使用rsync从本地到远程服务器保持"相同"目录名称
使用这种rsync语法,将目录及其内容复制到远程服务器后,我们将更改目录名称。
对于scp和rsync,逻辑保持不变
rsync语法:
rsync -avz <path to dir on localhost> user<@host>:<path on remote host>/
在这个例子中,我们将使用rsync将目录和内容从(/tmp/hynman
)复制到/home/temp
下的远程主机。
server1:~ # rsync -avz /tmp/hynman temp@server2:/home/temp/ Password: sending incremental file list hynman/ hynman/file1 hynman/file2 hynman/file3 hynman/test1/ hynman/test2/ hynman/test2/ sent 240 bytes received 81 bytes 49.38 bytes/sec total size is 0 speedup is 0.00
有关更多详细信息,请遵循rsync手册页。
其中
-a | --archive This is equivalent to -rlptgoD. It is a quick way of saying you want recursion and want to preserve almost everything (with -H being a notable omission). -r | --recursive This tells rsync to copy directories recursively -l | --links When symlinks are encountered, recreate the symlink on the destination. -p | --perms This option causes the receiving rsync to set the destination permissions to be the same as the source per‐ missions. -t | --times This tells rsync to transfer modification times along with the files and update them on the remote system. -g | --group This option causes rsync to set the group of the destination file to be the same as the source file. -o | --owner This option causes rsync to set the owner of the destination file to be the same as the source file, but only if the receiving rsync is being run as the super-user -D The -D option is equivalent to --devices --specials. -v | --verbose This option increases the amount of information you are given during the transfer. -z, --compress With this option, rsync compresses the file data as it is sent to the destination machine, which reduces the amount of data being transmitted -- something that is useful over a slow connection.
rsync复制后,请验证远程服务器上的传输。
因此,传输后,本地服务器和远程服务器上的"目录名称是相同的"。
server2:/home/temp # ll /home/temp/hynman/ total 8 -rw-r----- 1 temp users 0 Jan 10 16:38 file3 -rw-r----- 1 temp users 0 Jan 10 16:38 file2 -rw-r----- 1 temp users 0 Jan 10 16:38 file1 drwxr-x--- 2 temp users 4096 Jan 10 16:39 test2 drwxr-x--- 2 temp users 4096 Jan 10 16:39 test1
2.2:使用rsync将目录名称从本地更改为远程服务器
接下来,我们将使用rsync将/tmp/hynman
的目录和内容复制到/home/temp/rahul
中的远程服务器上的different folder
中。
因此,我们将目录名从localhost上的hynman更改为远程服务器上的rahul。
rsync语法:
rsync -avz <path to dir on localhost>/* user<@host>:<path on remote host>/
接下来以以下语法执行命令
server1:~ # rsync -avz /tmp/hynman/* temp@server2:/home/temp/rahul/ Password: sending incremental file list file1 file2 file3 test1/ test2/ sent 195 bytes received 77 bytes 108.80 bytes/sec total size is 0 speedup is 0.00
传输后,验证远程节点上的内容
server2:/home/temp # ls -l /home/temp/rahul/ total 8 -rw-r----- 1 temp users 0 Jan 10 16:38 file1 -rw-r----- 1 temp users 0 Jan 10 16:38 file2 -rw-r----- 1 temp users 0 Jan 10 16:38 file3 drwxr-x--- 2 temp users 4096 Jan 10 16:39 test1 drwxr-x--- 2 temp users 4096 Jan 10 16:39 test2
将目录和文件从远程复制到本地服务器
我们可以使用相同的工具scp递归和rsync将目录和内容从远程复制到Linux和Unix中的本地服务器。
虽然从ssh复制到本地的语法对于rsync和scp会有所不同
1. Linux将具有scp递归的目录和文件从远程复制到本地服务器
我们将再次使用scp recursive
在Linux和Unix中执行从远程到本地服务器的scp
要使用scp递归,我们必须使用带有-r
参数的scp。
1.1:使用scp从远程到本地服务器保留"相同"目录名称
检查scp语法以将文件从远程复制到本地服务器以了解更多详细信息
scp的语法:
scp -r <user><@host>:<source path on remote host> <destination on localhost>/
从远程到本地服务器的scp,下面是/home/temp/hynman下的远程主机(server2)上的内容,我希望将其复制到/tmp /下的本地主机(server1)上。
迪帕克
root@server2 ~]# ls -l /home/temp/hynman/ total 8 drwxr-xr-x 2 root root 4096 Jan 9 12:28 dir1 drwxr-xr-x 2 root root 4096 Jan 9 12:28 dir2 -rw-r--r-- 1 root root 0 Jan 9 12:28 file1 -rw-r--r-- 1 root root 0 Jan 9 12:28 file2 -rw-r--r-- 1 root root 0 Jan 9 12:28 file3
以下是在Linux和Unix中使用scp
将目录和内容从远程复制到本地服务器的命令
注意,在从远程目录复制到本地服务器后,我没有在源目录后提供"正斜杠"(/
),以保持相同的目录名。
[root@server1 ~]# scp -r root@server2:/home/temp/hynman /tmp/ Password: file2 100% 0 0.0KB/s 00:00 file3 100% 0 0.0KB/s 00:00 file1 100% 0 0.0KB/s 00:00
在复制内容的/tmp /下,验证localhostserver1
上的内容。
如我们所见,我们的本地主机(server1
)上现在有目录hynman
。
[root@server2 ~]# ls -l /home/temp/hynman/ total 8 drwxr-xr-x 2 root root 4096 Jan 9 12:28 dir1 drwxr-xr-x 2 root root 4096 Jan 9 12:28 dir2 -rw-r--r-- 1 root root 0 Jan 9 12:28 file1 -rw-r--r-- 1 root root 0 Jan 9 12:28 file2 -rw-r--r-- 1 root root 0 Jan 9 12:28 file3
1.2:使用scp将目录名从远程更改为本地服务器
要将目录和内容从远程复制到具有不同目录名的本地服务器,则必须谨慎使用正斜杠。
我们还必须在(server1
)的源路径中提供一个本地目录,要在该目录下从远程服务器复制文件和文件夹(server2
)
scp的语法:
scp -r <user><@host>:<source path on remote host>/* <destination on localhost>/
我们将在下一个scp示例中使用以下语法:
[root@server1 ~]# scp -r root@server2:/home/temp/hynman/* /tmp/rahul/ file1 100% 0 0.0KB/s 00:00 file2 100% 0 0.0KB/s 00:00 file3 100% 0 0.0KB/s 00:00
接下来,在/tmp/rahul
下验证localhostserver1
上的内容。
因此,来自" server2"的目录" hynman"的内容已成功复制到本地主机上的" rahul"下
[root@server1 ~]# ls -l /tmp/rahul/ total 8 drwxr-xr-x 2 root root 4096 Jan 9 12:43 dir1 drwxr-xr-x 2 root root 4096 Jan 9 12:43 dir2 -rw-r--r-- 1 root root 0 Jan 9 12:43 file1 -rw-r--r-- 1 root root 0 Jan 9 12:43 file2 -rw-r--r-- 1 root root 0 Jan 9 12:43 file3
2.使用rsync将文件夹和文件从远程复制到本地服务器
我们还可以使用rsync使用相同的参数但使用不同的语法将目录和内容从远程复制到本地服务器
要将文件和文件夹从远程复制到本地,我们必须在localhost上执行rsync,即为我们的环境使用server1
2.1:使用rsync将"相同"目录名称从远程服务器保留到本地服务器
注意rsync语法,我们在source path
中没有使用正斜杠(/
),因此将复制整个目录和内容
同样,我们可以使用下面的" rsync"命令将目录从远程复制到本地服务器
rsync的语法:
rsync -avz <user><@host>:<source path on remote host> <destination on localhost>/
使用以下命令,我们可以从远程文件夹到本地服务器
[root@server1 ~]# rsync -avz root@server2:/home/temp/hynman /tmp/ receiving incremental file list hynman/ hynman/file1 hynman/file2 hynman/file3 hynman/dir1/ hynman/dir2/ sent 97 bytes received 284 bytes 762.00 bytes/sec total size is 0 speedup is 0.00
验证server1上的/tmp/hynman的内容
[root@server1 ~]# ls -l /tmp/hynman/ total 8 drwxr-xr-x 2 root root 4096 Jan 9 12:28 dir1 drwxr-xr-x 2 root root 4096 Jan 9 12:28 dir2 -rw-r--r-- 1 root root 0 Jan 9 12:28 file1 -rw-r--r-- 1 root root 0 Jan 9 12:28 file2 -rw-r--r-- 1 root root 0 Jan 9 12:28 file3
2.2:使用rsync将目录名从远程更改为本地服务器
现在要更改目录名称或者将目录内容存储到远程服务器到本地服务器(" server1")的不同文件夹中,我们必须在远程服务器(" server2")的源路径中使用正斜杠。
检查rsync语法,如我们所见,我已经在(server2
)的源路径中定义了一个正斜杠。
rsync的语法:
rsync -avz <user><@host>:<source path on remote host>/* <destination on localhost>/
在这个rsync例子中,我将所有目录文件从(server2)上的/home/temp/hynman /
复制到本地主机(server1)上的
/tmp/rahul`中。
[root@server1 ~]# rsync -avz root@server2:/home/temp/hynman/* /tmp/rahul/ receiving incremental file list file1 file2 file3 dir1/ dir2/ sent 93 bytes received 238 bytes 662.00 bytes/sec total size is 0 speedup is 0.00
验证/tmp/rahul
下server1
上的内容
[root@server1 ~]# ls -l /tmp/rahul/ total 8 drwxr-xr-x 2 root root 4096 Jan 9 12:28 dir1 drwxr-xr-x 2 root root 4096 Jan 9 12:28 dir2 -rw-r--r-- 1 root root 0 Jan 9 12:28 file1 -rw-r--r-- 1 root root 0 Jan 9 12:28 file2 -rw-r--r-- 1 root root 0 Jan 9 12:28 file3
因此,传输成功,我希望我能够解释正斜杠在复制目录中所有文件以进行正确命名时的重要性。