32

I want to copy over .jpg and .png files with scp, but there files with different extensions in the same folder I'm copying from. I am doing the following:

scp user@someRemoteHost.com:/folder/*.{jpg,png} .

I am asked to enter my password for each extension type. Is there a way to do this in such a way that I enter my password only once?

αғsнιη
  • 41,407

2 Answers2

50

Just replace it with:

scp user@someRemoteHost.com:'/folder/*.{jpg,png}' .

Please note the pair of single quotes. In your case, your local shell is evaluating the expression, turning it really into:

scp user@someRemoteHost.com:/folder/*.jpg user@someRemoteHost.com:/folder/*.png .

hence the two passwords asked. In this solution, the pair of single quotes protects it from evaluation by the local shell, so it's the remote shell called by (the remote) scp which is evaluating the expression.

A.B
  • 36,364
  • 2
  • 73
  • 118
  • 2
    How can I include all subdirectories of that directory? – hola Jul 22 '19 at 14:54
  • @pushpen.paul If you don't need to select only png and jpg files but want to copy everything, then using for example scp -r user@someRemoteHost.com:'/folder' . should do it. Else if you still want to copy only png and jpg in those subdirectories you probably can't use scp or sftp (unless sftp/lftp with possible custom script) but need to run something like ssh + a pair of tar instead. You'd have to ask your own question with the specific needs then. – A.B Jul 24 '19 at 12:18
  • double quotes " also work besides single ones. – Timo Apr 28 '22 at 10:13
15

Better to use rsync for copying operations between servers.

 rsync -avzh user@remoteip:/path/*.jpg user@192.168.159.155:/path/*.png localserverpath

Using rsync it will asks for password only one time.

Also in rsync while transferring the file it will check in the target location if the file exists or not and also check whether content is same or not in source location and target location.

If file also exists in target location and the contents are also the same, then it won't copy that file. It will only copy the files which don't exist in the target location, so it reduces processing time.

rsync is often used as an incremental backup tool.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255