linux sftp一次下载多个文件,scp或sftp使用单个命令复制多个文件

该博客讨论如何使用SCP命令一次性复制多个文件到不同的远程目录。提到了使用通配符、brace expansion以及SFTP的方法,同时也包含了将文件从远程复制到本地的命令示例。还提及了通过SSH密钥对进行无密码登录以简化批处理操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

我想将文件从/向远程服务器复制到不同的目录中。例如,我想一次运行这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

最简单的方法是什么?

user987654 asked 2019-03-11T03:45:22Z

14个解决方案

305 votes

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

$ 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]

ios.id0 answered 2019-03-11T03:45:52Z

68 votes

从本地到服务器:

scp username@ip.of.server.copyfrom:"file1.log file2.log" "~/yourpathtocopy"

从服务器到本地:

scp username@ip.of.server.copyfrom:"file1.log file2.log" "~/yourpathtocopy"

Gtr_py answered 2019-03-11T03:46:31Z

52 votes

您可以使用-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 Kremser answered 2019-03-11T03:47:09Z

35 votes

正如Jiri所说,您可以使用sftp user@host -b batchFile.txt递归复制文件。 这假设有一个目录包含您要传输的所有文件(没有其他内容)。

但是,如果要从多个不同的目录传输文件,并且目标不相同,则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

alev answered 2019-03-11T03:47:48Z

14 votes

最简单的方法是

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

Tagar answered 2019-03-11T03:48:35Z

11 votes

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

真正的方法是:

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

Vouze answered 2019-03-11T03:49:08Z

10 votes

复制多个目录:

scp -r dir1 dir2 dir3 admin@127.0.0.1:~/

yuliskov answered 2019-03-11T03:49:33Z

9 votes

问题:使用单个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中也可以正常工作

kmario23 answered 2019-03-11T03:50:22Z

3 votes

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

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

~/Desktop$ scp -r your-user-id@remote.host.edu:Public/web/ Desktop/

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

~/Desktop$ scp -r your-user-id@remote.host.edu:Public/web/ Desktop/

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

~/Desktop$ scp -r your-user-id@remote.host.edu:Public/web/ Desktop/

fat43r 80ard answered 2019-03-11T03:51:20Z

2 votes

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

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

Reshmi K C answered 2019-03-11T03:51:52Z

1 votes

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

unxnut answered 2019-03-11T03:52:11Z

1 votes

就我而言,我只能使用sftp命令。

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

#!/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

dminear answered 2019-03-11T03:52:44Z

1 votes

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

scp user_name@ip.of.remote.machine:/some/log/folder/some_log_file.* ./

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

干杯,

家伙

Guy Avraham answered 2019-03-11T03:53:33Z

1 votes

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

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

请参阅什么是SSH-KEYGEN

augusto answered 2019-03-11T03:54:13Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值