scp或sftp使用单个命令复制多个文件

scp or sftp copy multiple files with single command

我想将文件从/向远程服务器复制到不同的目录中。
例如,我想一次运行这4个命令。

1
2
3
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

最简单的方法是什么?


将多个文件从远程复制到本地:

1
$ scp [email protected]:/some/remote/directory/\{a,b,c\} ./

将多个文件从本地复制到远程:

1
2
3
$ scp foo.txt bar.txt [email protected]:~
$ scp {foo,bar}.txt [email protected]:~
$ scp *.txt [email protected]:~

将多个文件从远程复制到远程:

1
2
$ scp [email protected]:/some/remote/directory/foobar.txt \
[email protected]:/some/remote/directory/

资料来源:http://www.hypexr.org/linux_scp_help.php


从本地到服务器:

scp file1.txt file2.sh [email protected]:~/pathtoupload

从服务器到本地:

scp -T [email protected]:"file1.txt file2.txt""~/yourpathtocopy"


您可以使用-r开关复制整个目录,这样如果您可以将文件隔离到自己的目录中,则可以一次复制所有内容。

1
2
3
scp -r ./dir-with-files user@remote-server:upload-path

scp -r user@remote-server:path-to-dir-with-files download-path

所以举个例子

1
scp -r [email protected]:/var/log ~/backup-logs

或者,如果只有少数,您可以使用:

1
scp 1.txt 2.txt 3.log user@remote-server:upload-path


正如Jiri所说,您可以使用scp -r user@host:/some/remote/path /some/local/path递归复制文件。这假设有一个目录包含您要传输的所有文件(没有其他内容)。

但是,如果要从多个不同的目录传输文件,并且目标不相同,则SFTP提供了另一种选择:

1
2
3
4
5
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


{file1,file2,file3}的答案仅适用于bash(在远程或本地)

真正的方法是:

1
scp user@remote:'/path1/file1 /path2/file2 /path3/file3' /localPath


最简单的方法是

1
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


在使用scp一段时间之后,我找到了最强大的解决方案:

(注意单引号和双引号)

本地到远程:

1
scp -r"FILE1""FILE2" HOST:'"DIR"'

远程到本地:

1
scp -r HOST:'"FILE1""FILE2"'"DIR"

请注意,"HOST:"之后的任何内容都将被发送到远程并在那里进行解析。所以我们必须确保它们不被本地shell处理。这就是单引号的原因。双引号用于处理文件名中的空格。

如果文件都在同一目录中,我们可以使用*来匹配它们,例如

1
2
scp -r"DIR_IN"/*.txt HOST:'"DIR"'
scp -r HOST:'"DIR_IN"/*.txt'"DIR"

与使用仅由某些shell支持的"{}"语法相比,这种语法是通用的


问题:使用单个SCP命令将多个目录从远程服务器复制到本地计算机,并保留远程服务器中的每个目录。

解决方案:SCP可以轻松完成此任务。这解决了在使用具有多个文件夹的SCP时多次输入密码的烦人问题。因此,这也节省了大量时间!

例如

1
2
3
4
5
6
7
# 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 [email protected]:/work/datasets/images/test/\{t1,t2,t3\}  .

PS:由这个伟大的答案激发:scp或sftp使用单个命令复制多个文件

根据评论,这在Windows上的Git Bash中也可以正常工作


复制多个目录:

1
scp -r dir1 dir2 dir3 [email protected]:~/


注意:我提前道歉,只回答上述问题的一部分。但是,我发现这些命令对我当前的unix需求很有用。

将特定文件从本地计算机上载到远程计算机:

~/Desktop/dump_files$ scp file1.txt file2.txt lab1.cpp etc.ext [email protected]:Folder1/DestinationFolderForFiles/

将整个目录从本地计算机上载到远程计算机:

<5233>

将整个目录从远程计算机下载到本地计算机:

~/Desktop$ scp -r [email protected]:Public/web/ Desktop/


就我而言,我只能使用sftp命令。
所以,我不得不使用带有sftp的批处理文件。我创建了一个如下的脚本。这假设您正在/ tmp目录中工作,并且您希望将文件放在远程系统上的destdir_on_remote_system中。这也适用于非交互式登录。您需要设置公钥/私钥,这样您无需输入密码即可登录。根据需要改变。

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash

cd /tmp
# start script with list of files to transfer
ls -1 fileset1* > batchfile1
ls -1 fileset2* >> batchfile1

sed -i -e 's/^/put /' batchfile1
echo"cd destdir_on_remote_system"> batchfile
cat batchfile1 >> batchfile
rm batchfile1

sftp -b batchfile user@host


你的命令工作得很完美,但我也希望在将本地发送到远程时更改文件名。我写了一个命令: -
sshpass -p password scp /path/to/file.txt root @ hostname:/path/newfile.txt

但它给出了错误
/path/newfile.txt:找不到这样的文件或目录
PLZ在这种情况下帮助我


scp使用ssh进行相同身份验证的数据传输,并提供与ssh相同的安全性。

这里的最佳实践是实现"SSH密钥和公共密钥认证"。有了这个,您可以编写脚本而无需担心身份验证。就那么简单。

请参阅什么是SSH-KEYGEN


在所有文件具有相同扩展名但具有不同后缀(例如日志文件数)的特定情况下,您将使用以下内容:

1
scp [email protected]:/some/log/folder/some_log_file.* ./

这将从远程的给定文件夹中复制名为some_log_file的所有文件,即some_log_file.1,some_log_file.2,some_log_file.3 ....


1
scp remote:"[A-C]/[12].txt" local: