我想复制文件从/到远程服务器在不同的目录。 例如,我想一次运行这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

其他回答

从远程复制多个文件到本地:

$ scp your_username@remote.edu:/some/remote/directory/\{a,b,c\} ./

从本地复制多个文件到远程:

$ scp foo.txt bar.txt your_username@remotehost.edu:~
$ scp {foo,bar}.txt your_username@remotehost.edu:~
$ scp *.txt your_username@remotehost.edu:~

从远程复制多个文件到远程:

$ scp your_username@remote1.edu:/some/remote/directory/foobar.txt \
your_username@remote2.edu:/some/remote/directory/

来源:http://www.hypexr.org/linux_scp_help.php

从本地到服务器:

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

从服务器到本地:

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

在我的例子中,我被限制只能使用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

注意:我很抱歉只回答了上面问题的一部分。但是,我发现这些命令对我当前的unix需求很有用。

从本地机器上传特定文件到远程机器:

~/桌面/dump_file $ scp file1.txt file2.txt lab1.cpp等人

从本地机器上传整个目录到远程机器:

~$ scp -r Desktop/dump_files your-user-id@remotemachine.edu:Folder1/DestinationFolderForFiles/

从远程机器下载整个目录到本地机器:

~/Desktop$ scp -r your-user-id@remote.host.edu:Public/web/ Desktop/

不使用scp更简单:

tar cf - file1 ... file_n | ssh user@server 'tar xf -'

这还允许您执行一些操作,例如压缩流(-C)或(自OpenSSH v7.3以来)-J,以便随时在一个(或多个)代理服务器之间跳转。

通过将公钥对应到~/来避免使用密码。Ssh /authorized_keys(在服务器端)与Ssh -copy-id(在客户端)。

也张贴在这里(有更多细节)和这里。