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 [email protected]:/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?

3 Answers 3

50

Just replace it with:

scp [email protected]:'/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 [email protected]:/folder/*.jpg [email protected]:/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.

3
  • 2
    How can I include all subdirectories of that directory?
    – hola
    Commented Jul 22, 2019 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 [email protected]:'/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
    Commented Jul 24, 2019 at 12:18
  • double quotes " also work besides single ones.
    – Timo
    Commented Apr 28, 2022 at 10:13
14

Better to use rsync for copying operations between servers.

 rsync -avzh user@remoteip:/path/*.jpg [email protected]:/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.

1
  • 12
    No matter if used rsync or scp, the point is that /folder/*.{jpg,png} should quote '/folder/*.{jpg,png}' . Commented Jan 16, 2018 at 11:37
1

Someone in the comments asked about getting files from all the subdirectories as well and A.B suggested ssh + tar. Just to give an explicit solution for this you could use (with zip instead):

ssh user@ip_address 'cd /path/to/folder/; find . "(" -iname "*.png" -o -iname "*.jpg" ")" -exec zip -r all_files.zip {} +'; scp user@ip_address:/path/to/folder/all_files.zip .

Thank you to @G-Man Says 'Reinstate Monica' for refining the original solution into a more robust+portable solution :)

Note: the cd here is so you don't get a zip file that has the entire path from the root directory when you unzip

4
  • Very interesting, thank you for sharing @G-ManSays'ReinstateMonica'! I didn't think about this. It seems that the proper way to do this would be using find . -name "*.png" -or -name "*.jpg" -exec zip -r all_files.zip {} +. Do you see any issues with this?
    – user609243
    Commented Apr 30 at 18:21
  • (1) I believe that you need parentheses.  (2) -or is a non-portable extension. The standard is -o. So: find . "(" -name "*.png" -o -name "*.jpg" ")" -exec zip -r all_files.zip {} + should work.  (3) You might want to use -iname to match *.PNG, etc., as well. Commented Apr 30 at 18:54
  • Ah, I see... the command worked on my Mac but on Ubuntu I needed to add the parenthesis you mentioned. I'll update my answer with your fully portable command. Thank you for your guidance!
    – user609243
    Commented May 7 at 19:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .