我想将文件从/向远程服务器复制到不同的目录中.例如,我想一次运行这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
Run Code Online (Sandbox Code Playgroud)
最简单的方法是什么?
小智 374
将多个文件从远程复制到本地:
$ scp your_username@remote.edu:/some/remote/directory/\{a,b,c\} ./
Run Code Online (Sandbox Code Playgroud)
将多个文件从本地复制到远程:
$ scp foo.txt bar.txt your_username@remotehost.edu:~
$ scp {foo,bar}.txt your_username@remotehost.edu:~
$ scp *.txt your_username@remotehost.edu:~
Run Code Online (Sandbox Code Playgroud)
将多个文件从远程复制到远程:
$ scp your_username@remote1.edu:/some/remote/directory/foobar.txt \
your_username@remote2.edu:/some/remote/directory/
Run Code Online (Sandbox Code Playgroud)
资料来源:http://www.hypexr.org/linux_scp_help.php
Gtr*_*_py 100
从本地到服务器:
scp file1.txt file2.sh username@ip.of.server.copyto:~/pathtoupload
从服务器到本地:
scp -T username@ip.of.server.copyfrom:"file1.txt file2.txt" "~/yourpathtocopy"
Jir*_*ser 63
您可以使用-r
switch 复制整个目录,因此如果您可以将文件隔离到自己的目录中,则可以一次复制所有内容.
scp -r ./dir-with-files user@remote-server:upload-path
scp -r user@remote-server:path-to-dir-with-files download-path
Run Code Online (Sandbox Code Playgroud)
所以举个例子
scp -r root@192.168.1.100:/var/log ~/backup-logs
Run Code Online (Sandbox Code Playgroud)
或者,如果只有少数,您可以使用:
scp 1.txt 2.txt 3.log user@remote-server:upload-path
Run Code Online (Sandbox Code Playgroud)
ale*_*lev 46
正如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
Run Code Online (Sandbox Code Playgroud)
这使用"here doc"语法来定义SFTP输入命令序列.作为替代方案,您可以将SFTP命令放入文本文件并执行sftp user@host -b batchFile.txt
Vou*_*uze 22
答案{file1,file2,file3}
仅适用于bash(在远程或本地)
真正的方法是:
scp user@remote:'/path1/file1 /path2/file2 /path3/file3' /localPath
Run Code Online (Sandbox Code Playgroud)
Tag*_*gar 14
最简单的方法是
local$ scp remote:{A/1,A/2,B/3,C/4}.txt ./
Run Code Online (Sandbox Code Playgroud)
所以{..}列表可以包含目录(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
eos*_*rox 14
与scp玩了一段时间后,我发现了最可靠的解决方案:
(请注意单引号和双引号)
本地到远程:
scp -r "FILE1" "FILE2" HOST:'"DIR"'
Run Code Online (Sandbox Code Playgroud)
远程到本地:
scp -r HOST:'"FILE1" "FILE2"' "DIR"
Run Code Online (Sandbox Code Playgroud)
请注意,“ HOST:”之后的所有内容都将发送到远程并在那里进行解析。因此,我们必须确保本地外壳程序不会处理它们。这就是为什么使用单引号引起来的原因。双引号用于处理文件名中的空格。
如果文件都在同一目录中,我们可以使用*来匹配它们,例如
scp -r "DIR_IN"/*.txt HOST:'"DIR"'
scp -r HOST:'"DIR_IN"/*.txt' "DIR"
Run Code Online (Sandbox Code Playgroud)
与使用某些外壳程序仅支持的“ {}”语法相比,该语法是通用的
yul*_*kov 13
复制多个目录:
scp -r dir1 dir2 dir3 admin@127.0.0.1:~/
Run Code Online (Sandbox Code Playgroud)
kma*_*o23 12
问题:使用单个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\} .
Run Code Online (Sandbox Code Playgroud)
PS:由这个伟大的答案激发:scp或sftp使用单个命令复制多个文件
根据评论,这在Windows上的Git Bash中也可以正常工作
你可以这样做:
scp hostname@serverNameOrServerIp:/path/to/files/\\{file1,file2,file3\\}.fileExtension ./
Run Code Online (Sandbox Code Playgroud)
这会将所有列出的文件名下载到您所在的任何本地目录。
确保不要在每个文件名之间放置空格,只使用逗号,
。
不使用更简单scp
:
tar cf - file1 ... file_n | ssh user@server 'tar xf -'
Run Code Online (Sandbox Code Playgroud)
这还允许您执行一些操作,例如压缩流 ( -C
) 或(自 OpenSSH v7.3 起)-J
通过一个(或多个)代理服务器随时跳转。
通过将您的公钥复制到~/.ssh/authorized_keys
(服务器上)和ssh-copy-id
(客户端上)来避免使用密码。
归档时间: |
|
查看次数: |
323977 次 |
最近记录: |