scp files from multiple directories on local to multiple directories on remote in one command
You can't have multiple destinations in one scp
command. If you want to make a single SSH connection, you'll need to use some other tool.
The simplest solution is to mount the remote filesystem over SSHFS and then use the cp
command. This requires SFTP access.
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
Another solution is to first organize the files locally, then copy the hierarchy. 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 username@host_server:/file
rm -r destination1 destination2 destination3
Another solution is to keep using scp
, but first open a master connection to the server. This is explained in Using an already established SSH channel
Alternatively, just bear with it and make three scp
connections. But don't use your password to log in; instead, create a key pair and load the private key into your key agent (ssh-add ~/.ssh/id_rsa
), then you won't have to type anything on each connection.