我想复制文件从/到远程服务器在不同的目录。 例如,我想一次运行这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
最简单的方法是什么?
当前回答
问题:使用单个SCP命令将多个目录从远程服务器复制到本地计算机,并保留每个目录在远程服务器中的原样。
解决方案:SCP可以很容易地做到这一点。这解决了在对多个文件夹使用SCP时需要多次输入密码的烦人问题。因此,这也节省了很多时间!
e.g.
# 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中也可以正常工作
其他回答
你可以这样做:
scp hostname@serverNameOrServerIp:/path/to/files/\\{file1,file2,file3\\}.fileExtension ./
这将把所有列出的文件名下载到您所在的本地目录。
确保每个文件名之间不要有空格,只使用逗号。
在我的例子中,我被限制只能使用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
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
scp remote:"[A-C]/[12].txt" local:
你可以使用-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