我想复制文件从/到远程服务器在不同的目录。 例如,我想一次运行这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 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 remote:"[A-C]/[12].txt" local:
你可以这样做:
scp hostname@serverNameOrServerIp:/path/to/files/\\{file1,file2,file3\\}.fileExtension ./
这将把所有列出的文件名下载到您所在的本地目录。
确保每个文件名之间不要有空格,只使用逗号。
复制多个目录:
scp -r dir1 dir2 dir3 admin@127.0.0.1:~/
在玩了一段时间scp之后,我找到了最健壮的解决方案:
(注意单引号和双引号)
本地到远端:
scp -r "FILE1" "FILE2" HOST:'"DIR"'
远程到本地:
scp -r HOST:'"FILE1" "FILE2"' "DIR"
注意,“HOST:”后面的内容将被发送到远程服务器并在那里进行解析。所以我们必须确保它们没有被本地shell处理。这就是使用单引号的原因。双引号用于处理文件名中的空格。
如果所有文件都在同一个目录中,我们可以使用*来匹配它们,例如
scp -r "DIR_IN"/*.txt HOST:'"DIR"'
scp -r HOST:'"DIR_IN"/*.txt' "DIR"
与使用只有某些shell支持的“{}”语法相比,这个语法是通用的
问题:使用单个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中也可以正常工作