How to copy multiple files to different remote directories with one command


Use cases

If you want to copy multiple files to different remote directories, SCP is usually used. But SCP does not allow you to specify multiple target addresses on a single command, so you have to use multiple SCP commands.

scp -v /file/source1/* [email protected]_server:/file/destination1
scp -v /file/source2/* [email protected]_server:/file/destination2
scp -v /file/source3/* [email protected]_server:/file/destination3

How to copy multiple files to different remote directories with one command?

Scheme 1

The simplest solution is to useSSHFSMap the remote address to the local, and then use the CP command. This requires access to SFTP.

mkdir host_server
sshfs [email protected]_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

Scheme 2

Another solution is to organize the files locally, and then copy the whole structure. This requires 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 [email protected]_server:/file
rm -r destination1 destination2 destination3

Scheme 3

The third is to continue to use SCP, but you need to open a master connection to point to the server first.
Add in ~ /. SSH / config

ControlMaster auto
ControlPath ~/.ssh/control:%h:%p:%r

If you start an SSH session with the same user, port, machine as the existing connection, the second session will be tunneled to the first one. Establishing a second connection does not require re authentication and is fast.

Scheme 4

You can continue to use SCP, but avoid entering your user name and password every time.
Create a key pair and inject the private key into the key agentssh-add ~/.ssh/id_rsa, so you don’t have to type anything every time you connect.

However, options 3 and 4 are only a compromise.

reference resources:…

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]