我需要将一些文件从多个本地目录复制到多个远程目录。
命令:
scp -v /file/source1/* username@host_server:/file/destination1
scp -v /file/source2/* username@host_server:/file/destination2
scp -v /file/source3/* username@host_server:/file/destination3
Run Code Online (Sandbox Code Playgroud)
它一次又一次地要求输入密码。
命令:
scp file1 file2 ... fileN user@host:/destination/directory/
Run Code Online (Sandbox Code Playgroud)
将所有文件放入一个目标目录。
但是我的目的地对于所有文件都不同。
一个命令中不能有多个目的地scp
。如果您想建立单个 SSH 连接,则需要使用其他工具。
最简单的解决方案是通过SSHFS挂载远程文件系统,然后使用该cp
命令。这需要 SFTP 访问。
mkdir host_server
sshfs username@host_server:/file host_server
cp /file/source1/* host_server/destination1
cp /file/source2/* host_server/destination2
cp /file/source3/* host_server/destination3
fusermount -u host_server
rmdir host_server
Run Code Online (Sandbox Code Playgroud)
另一种解决方案是首先在本地组织文件,然后复制层次结构。这需要rsync。
mkdir destination1 destination2 destination3
ln -s /file/source1/* destination1
ln -s /file/source2/* destination2
ln -s /file/source3/* destination3
rsync -a --copy-unsafe-links destination1 destination2 destination3 username@host_server:/file
rm -r destination1 destination2 destination3
Run Code Online (Sandbox Code Playgroud)
另一个解决方案是继续使用scp
,但首先打开到服务器的主连接。使用已建立的 SSH 通道对此进行了解释
或者,只需忍耐并建立三个scp
连接。但不要使用您的密码登录;相反,创建一个密钥对并将私钥加载到您的密钥代理 ( ssh-add ~/.ssh/id_rsa
) 中,然后您无需在每个连接上键入任何内容。