我想从/向不同目录的远程服务器复制文件。 例如,我希望同时运行这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/
~
或./
。 - aesede\{a,b,c\}
将在单个连接/批次中传输文件(因为它们将在远程主机上展开),而使用{a,b,c}
将打开多个连接,并且在传输许多文件时开销非常明显。 - omninonsense从本地上传到服务器:
scp file1.txt file2.sh username@ip.of.server.copyto:~/pathtoupload
从服务器下载到本地(OpenSSH v9.0之前):
scp -T username@ip.of.server.copyfrom:"file1.txt file2.txt" "~/yourpathtocopy"
从服务器下载到本地(OpenSSH v9.0+):
scp -OT username@ip.of.server.copyfrom:"file1.txt file2.txt" "~/yourpathtocopy"
参考man 1 scp
:
-O Use the legacy SCP protocol for file transfers instead of the SFTP protocol. Forcing the use of the
SCP protocol may be necessary for servers that do not implement SFTP, for backwards-compatibility for
particular filename wildcard patterns and for expanding paths with a ‘~’ prefix for older SFTP
servers.
HISTORY
Since OpenSSH 9.0, scp has used the SFTP protocol for transfers by default.
Get-Command scp
获取的版本7.7.2.1
中似乎没有-T
选项。不过即使不加-T
,该命令也可以正常工作。 - dtasev-r
开关复制整个目录,因此如果你可以将文件隔离到自己的目录中,就可以一次性复制所有内容。scp -r ./dir-with-files user@remote-server:upload-path
scp -r user@remote-server:path-to-dir-with-files download-path
例如,
scp -r root@192.168.1.100:/var/log ~/backup-logs
或者如果它们只有几个,您可以使用:
scp 1.txt 2.txt 3.log user@remote-server:upload-path
正如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
/home/vagrant/Code/
而不是~/Code/
)之前,我遇到了一些麻烦。 - Ryan使用{file1,file2,file3}
的答案仅适用于bash(远程或本地)
真正的方法是:
scp user@remote:'/path1/file1 /path2/file2 /path3/file3' /localPath
{…, …, …}
进行文件名扩展在许多 shell 中都可以工作,而不仅仅是 Bash。然而,这种方法在任何 shell 中都可以工作,因此显然更好。 - Piotr Dobrogost-T
选项。 - Alex Barker玩了一段时间的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 -r robwaa@192.168.1.101:'~/"bin/set-vpn-kill-switch" ~/"bin/flush-iptables"' ~/""
- Rob Waa问题: 使用单个SCP命令将多个目录从远程服务器复制到本地计算机,并保留每个目录在远程服务器中的状态。
解决方案: SCP可以轻松实现此操作。这解决了使用SCP与多个文件夹时需要多次输入密码的烦恼。因此,这也节省了大量时间!
例如:
# 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也可以正常工作
最简单的方法是
local$ scp remote:{A/1,A/2,B/3,C/4}.txt ./
所以{..}列表可以包括目录(这里的A、B和C是目录;"1.txt"和"2.txt"是这些目录中的文件名)。
虽然它会将这四个文件都复制到一个本地目录中——不确定这是否符合您的要求。
在上述情况下,您最终将远程文件A/1.txt、A/2.txt、B/3.txt和C/4.txt复制到单个本地目录中,并使用文件名./1.txt、./2.txt、./3.txt和./4.txt。
sh
。 - tripleeelocal$ scp -r remote:{A/1.txt,A/2.txt,B/3.txt,C/4.txt,D,F} ./
- donhector您可以这样做:
scp hostname@serverNameOrServerIp:/path/to/files/\\{file1,file2,file3\\}.fileExtension ./
这将会下载所有列出的文件名到你所在的本地目录。
请确保不要在每个文件名之间放置空格,只需使用逗号,
隔开。
/path/to/files/\{file1.ext,file2.ext,file3.ext\}
。注意,对于PowerShell人员(现在微软已经加入了这一行列),您需要使用\``进行转义:
/path/to/files/`{file1.ext,file2.ext,file3.ext`}`。 - dtasevscp
命令传输多个相同扩展名的文件?我尝试过"{file1,file2}".pdf
和{"file1,file2"}.pdf
,但都无法正常工作:提示“没有这样的文件或目录”。然而,对于单个文件,您提供的第一个选项可以正常工作。但是,对于多个文件,应该怎么做呢?请帮忙解决! - Pathros复制多个目录:
scp -r dir1 dir2 dir3 admin@127.0.0.1:~/
rsync
,它可能有助于你处理当前和许多未来的情况。为了避免输入密码(更不用说多次了),你应该了解ssh
公钥/私钥,例如 https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2 - mnagelscp root@192.168.56.120:'/etc/openvpn/ca.crt /etc/openvpn/client/client0.crt /etc/openvpn/client/client0.key /etc/openvpn/client/ta.key' ./
。 - Eduardo Lucio