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 

什么是最简单的方法来做到这一点?

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

 $ 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 username@ip.of.server.copyfrom:"file1.log file2.log" "~/yourpathtocopy"

您可以使用-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/pathrecursion地复制文件。 这假设有一个包含所有你想传输的文件的目录(没有别的)。

但是,如果要从多个不同的目录传输文件,并且目标不相同,则SFTP会提供一个替代scheme:

 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”语法来定义一系列SFTPinput命令。 作为替代,您可以将SFTP命令放入一个文本文件并执行sftp user@host -b batchFile.txt

最简单的方法是

 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 -r dir1 dir2 dir3 admin@127.0.0.1:~/ 

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

将特定文件从本地机器上传到远程机器:

~/Desktop/dump_files$ scp file1.txt file2.txt lab1.cpp etc.ext your-user-id@remotemachine.edu:Folder1/DestinationFolderForFiles/

将整个目录从本地机器上传到远程机器:

~$ scp -r Desktop/dump_files your-user-id@remotemachine.edu:Folder1/DestinationFolderForFiles/

从远程机器下载整个目录到本地机器:

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

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

但是它给了错误,/ path / newfile.txt:没有这样的文件或目录findPLZ帮助我在这种情况下

 scp remote:"[AC]/[12].txt" local: 

在所有文件具有相同扩展名但具有不同后缀(比如说日志文件号)的特定情况下,使用以下命令:

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

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

干杯,

家伙

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

解决scheme :SCP可以轻松做到这一点。 这解决了在多个文件夹中使用SCP时多次input密码的麻烦问题。

例如

 # 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/datasetshttp://img.dovov.comtest/\{t1,t2,t3\} . 

PS:受到这个伟大答案的启发。 谢谢!

在我的情况下,我只限于使用sftp命令。
所以,我不得不使用sftp的batch file。 我创build了如下的脚本。 这假设你正在/ tmp目录下工作,并且你想把文件放在远程系统的destdir_on_remote_system中。 这也只适用于非交互式login。 您需要设置公钥/私钥,以便您无需input密码即可login。 根据需要更改。

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

在远程服务器上

 tar -czf ~/files.tar.gz A/1.txt A/2.txt B/1.txt C/1.txt exit 

在本地机器上

 scp remote:files.tar.gz . tar -xzf files.tar.gz rm files.tar.gz