我想复制文件从/到远程服务器在不同的目录。 例如,我想一次运行这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
最简单的方法是什么?
当前回答
正如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 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"
{file1,file2,file3}的答案仅适用于bash(在远程或本地)
真正的方法是:
scp user@remote:'/path1/file1 /path2/file2 /path3/file3' /localPath
在玩了一段时间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支持的“{}”语法相比,这个语法是通用的
注意:我很抱歉只回答了上面问题的一部分。但是,我发现这些命令对我当前的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/