我想复制文件从/到远程服务器在不同的目录。 例如,我想一次运行这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
其他回答
serverHomeDir='/home/somepath/ftp/'
backupDirAbsolutePath=${serverHomeDir}'_sqldump_'
backupDbName1='2021-08-27-03-56-somesite-latin2.sql'
backupDbName2='2021-08-27-03-56-somesite-latin1.sql'
backupDbName3='2021-08-27-03-56-somesite-utf8.sql'
backupDbName4='2021-08-27-03-56-somesite-utf8mb4.sql'
scp -i ~/.ssh/id_rsa.pub user@server.domain.com:${backupDirAbsolutePath}/"{$backupDbName1,$backupDbName2,$backupDbName3,$backupDbName4}" .
. -在最后将文件下载到当前目录
我~ / . ssh / id_rsa。Pub -假设您使用.pub密钥建立了服务器的SSH
{file1,file2,file3}的答案仅适用于bash(在远程或本地)
真正的方法是:
scp user@remote:'/path1/file1 /path2/file2 /path3/file3' /localPath
注意:我很抱歉只回答了上面问题的一部分。但是,我发现这些命令对我当前的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/
正如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使用SSH进行数据传输,并提供与SSH相同的身份验证和安全性。
这里的最佳实践是实现“SSH密钥和公钥身份验证”。这样,您就可以不用担心身份验证就可以编写脚本。就这么简单。
看看什么是SSH-KEYGEN