我想复制文件从/到远程服务器在不同的目录。 例如,我想一次运行这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 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
其他回答
正如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 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.
在我的例子中,我被限制只能使用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
从远程复制多个文件到本地:
$ 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
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