I need to copy some files from multiple local directories to multiple remote directories.
The command:
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
It asks for password again and again.
The command:
scp file1 file2 ... fileN user@host:/destination/directory/
Will put all the files into one destination directory.
But my destination are different for all the files.
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.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments