I would like to know an easy way to use scp to copy files and folders that are present in different paths on my file system. The ssh destination server requests a password and I
Alternatively, if you cannot use public key authentication, you may add the following configuration to SSH (either to ~/.ssh/config
or as the appropriate command-line arguments):
ControlMaster auto
ControlPath /tmp/ssh_mux_%h_%p_%r
ControlPersist 2m
With this config, the SSH connection will be kept open for 2 minutes so you'll only need to type the password the first time.
This post has more details on this feature.
If you can express all the names of the files you want to copy from the remote system using a single glob pattern, then you can do this in a single scp
command. This usage will only support a single destination folder on the local system for all files though. For example:
scp 'RemoteHost:/tmp/[abc]*/*.tar.gz' .
copies all of the files from the remote system which are names (something).tar.gz
and which are located in subdirectories of /tmp
whose names begin with a
, b
, or c
. The single quotes are to protect the glob pattern from being interpreted from the shell on the local system.
If you cannot express all the files you want to copy as a single glob pattern and you still want the copy to be done using a single command (and a single SSH connection which will ask for your passsword only once) then you can either:
scp
, like sftp
or rsync
, orFrom this site:
SSHSOCKET=~/.ssh/myUsername@targetServerName
ssh -M -f -N -o ControlPath=$SSHSOCKET myUsername@targetServerName
scp -o ControlPath=$SSHSOCKET myUsername@targetServerName:remoteFile.txt ./
ssh -S $SSHSOCKET -O exit myUsername@targetServerName
It's intuitive, safer than creating a key pair, faster than creating a compressed file and worked for me!
in addition to the already mentioned glob:
you can use {,}
to define alternative paths/pathparts in one single statement
e.g.: scp user@host:/{PATH1,PATH2} DESTINATION
create a key pair, copy the public key to the server side.
ssh-keygen -t rsa
Append content inside the file ~/.ssh/identity.pub to file ~/.ssh/authorized_keys2 of server side user. You need not to type password anymore.
However, be careful! anybody who can access your "local account" can "ssh" to the server without password as well.