我想复制文件从/到远程服务器在不同的目录。 例如,我想一次运行这4个命令。

scp remote:A/1.txt local:A/1.txt
scp remote:A/2.txt local:A/2.txt
scp remote:B/1.txt local:B/1.txt
scp remote:C/1.txt local:C/1.txt

最简单的方法是什么?


当前回答

在我的例子中,我被限制只能使用sftp命令。 所以,我必须使用带有sftp的批处理文件。我创建了如下所示的脚本。这假设您在/tmp目录下工作,并且希望将文件放在远程系统的destdir_on_remote_system中。这也只适用于非交互式登录。您需要设置公钥/私钥,以便无需输入密码即可登录。根据需要进行更改。

#!/bin/bash

cd /tmp
# start script with list of files to transfer
ls -1 fileset1* > batchfile1
ls -1 fileset2* >> batchfile1

sed -i -e 's/^/put /' batchfile1
echo "cd destdir_on_remote_system" > batchfile
cat batchfile1 >> batchfile
rm batchfile1

sftp -b batchfile user@host

其他回答

在我的例子中,我被限制只能使用sftp命令。 所以,我必须使用带有sftp的批处理文件。我创建了如下所示的脚本。这假设您在/tmp目录下工作,并且希望将文件放在远程系统的destdir_on_remote_system中。这也只适用于非交互式登录。您需要设置公钥/私钥,以便无需输入密码即可登录。根据需要进行更改。

#!/bin/bash

cd /tmp
# start script with list of files to transfer
ls -1 fileset1* > batchfile1
ls -1 fileset2* >> batchfile1

sed -i -e 's/^/put /' batchfile1
echo "cd destdir_on_remote_system" > batchfile
cat batchfile1 >> batchfile
rm batchfile1

sftp -b batchfile user@host
scp -r root@ip-address:/root/dir/ C:\Users\your-name\Downloads\

使用-r可以下载远程服务器的dir目录下的所有文件

在我的案例中,有太多名称不相关的文件。

我最后用了,

$  for i in $(ssh remote 'ls ~/dir'); do scp remote:~/dir/$i ./$i; done
1.txt                                         100%  322KB   1.2MB/s   00:00
2.txt                                         100%   33KB 460.7KB/s   00:00
3.txt                                         100%   61KB 572.1KB/s   00:00

$

在特定的情况下,所有文件具有相同的扩展名,但具有不同的后缀(例如日志文件的数量),您可以使用以下方法:

scp user_name@ip.of.remote.machine:/some/log/folder/some_log_file.* ./

这将从远程文件中的给定文件夹中复制所有名为some_log_file的文件,即- some_log_file。1, some_log_file。2, some_log_file。3.

从本地到服务器:

SCP file1.txt file2.sh username@ip.of.server.copyto:~/pathtoupload

从服务器到本地:

scp -T username@ip.of.server.copyfrom:"file1.txt file2.txt" "~/yourpathtocopy"