我想复制文件从/到远程服务器在不同的目录。 例如,我想一次运行这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
最简单的方法是什么?
我想复制文件从/到远程服务器在不同的目录。 例如,我想一次运行这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
最简单的方法是什么?
当前回答
问题:使用单个SCP命令将多个目录从远程服务器复制到本地计算机,并保留每个目录在远程服务器中的原样。
解决方案:SCP可以很容易地做到这一点。这解决了在对多个文件夹使用SCP时需要多次输入密码的烦人问题。因此,这也节省了很多时间!
e.g.
# copies folders t1, t2, t3 from `test` to your local working directory
# note that there shouldn't be any space in between the folder names;
# we also escape the braces.
# please note the dot at the end of the SCP command
~$ cd ~/working/directory
~$ scp -r username@contact.server.de:/work/datasets/images/test/\{t1,t2,t3\} .
PS:受到这个伟大答案的激励:scp或sftp用一个命令复制多个文件
根据注释,这在Windows上的Git Bash中也可以正常工作
其他回答
SCP使用SSH进行数据传输,并提供与SSH相同的身份验证和安全性。
这里的最佳实践是实现“SSH密钥和公钥身份验证”。这样,您就可以不用担心身份验证就可以编写脚本。就这么简单。
看看什么是SSH-KEYGEN
scp remote:"[A-C]/[12].txt" local:
正如Jiri提到的,可以使用scp -r user@host:/some/remote/path /some/local/path递归地复制文件。这假设有一个目录包含所有您想要传输的文件(没有其他文件)。
但是,如果你想从多个不同的目录传输文件,并且目的地不相同,SFTP提供了一种替代方案:
sftp user@host << EOF
get /some/remote/path1/file1 /some/local/path1/file1
get /some/remote/path2/file2 /some/local/path2/file2
get /some/remote/path3/file3 /some/local/path3/file3
EOF
这里使用“here doc”语法定义一系列SFTP输入命令。作为一种替代方法,您可以将SFTP命令放在一个文本文件中,并执行SFTP user@host -b batchFile.txt
不使用scp更简单:
tar cf - file1 ... file_n | ssh user@server 'tar xf -'
这还允许您执行一些操作,例如压缩流(-C)或(自OpenSSH v7.3以来)-J,以便随时在一个(或多个)代理服务器之间跳转。
通过将公钥对应到~/来避免使用密码。Ssh /authorized_keys(在服务器端)与Ssh -copy-id(在客户端)。
也张贴在这里(有更多细节)和这里。
问题:使用单个SCP命令将多个目录从远程服务器复制到本地计算机,并保留每个目录在远程服务器中的原样。
解决方案:SCP可以很容易地做到这一点。这解决了在对多个文件夹使用SCP时需要多次输入密码的烦人问题。因此,这也节省了很多时间!
e.g.
# copies folders t1, t2, t3 from `test` to your local working directory
# note that there shouldn't be any space in between the folder names;
# we also escape the braces.
# please note the dot at the end of the SCP command
~$ cd ~/working/directory
~$ scp -r username@contact.server.de:/work/datasets/images/test/\{t1,t2,t3\} .
PS:受到这个伟大答案的激励:scp或sftp用一个命令复制多个文件
根据注释,这在Windows上的Git Bash中也可以正常工作