150

Why can't I copy with scp when I'm using * characters in the path?

scp SERVERNAME:/DIR/* .

What configuration does SCP need in order to allow * in the path?

UPDATE: the problem is not on server side; pscp is trying to use SCPv1, and that's why the error message:

scp commandline and error message regarding a file called *

  • Check this, it worked for me: https://superuser.com/a/1390405/445746 – AmitM Jan 4 '19 at 7:26
  • The question isn't clear as to whether you're trying to use * for file globbing, or to copy a file called * (see disagreement in comments as to what the question is asking). – LarsH Nov 26 '19 at 15:41
131

You need to pass a literal escape to scp to avoid the remote machine treating * as a glob (notice that it is doubly quoted):

scp 'SERVERNAME:/DIR/\*' .
  • 46
    You either need quotes, or a backslash before the star, not both. And scp is not the one expanding it, the shell is. – Patrick Dec 23 '11 at 4:39
  • 3
    Have you tried it? I just did, works exactly as I described (quoting and escaping causes failure). User1274964 even confirms the behavior in his answer. touch /tmp/abcd.1234; scp 'localhost:/tmp/abcd.\*' ./: scp: /tmp/abcd.*: No such file or directory – Patrick Dec 7 '13 at 1:00
  • 2
    @Patrick The question is about using a literal asterisk, ie, avoiding globbing altogether, not about how to glob on the remote. – Chris Down Dec 8 '13 at 20:57
  • 2
    Actually the question is about getting pscp on the client side to allow remote globbing. – Patrick Dec 8 '13 at 22:08
  • 1
    Yep you do not need to escape the asterisk if you're using the single quotes – Led Oct 23 '18 at 15:22
59

I found Patrick's advice to be correct, although Chris's answer got me on the right track. Use quotes and then you don't need the backslash before the asterisk.

scp 'SERVERNAME:/tmp/file_num\*' .

scp: /tmp/file_num*.csv: No such file or directory

scp 'SERVERNAME:/tmp/file_num*' .

judgments_for_job_171642.csv 100% 32KB 32.0KB/s 00:00
judgments_for_job_172394.csv 100% 548KB 182.6KB/s 00:03

  • 4
    Right, because this means that you want to glob on the remote. The question is about how to stop globbing on the remote (to get a literal *), not about how to glob on the remote only. – Chris Down Dec 8 '13 at 20:57
  • 3
    @ChrisDown. No. The user wants the wildcard not expanded on the host, and does want it expanded on the remote, to allow it to match multiple files there. None of the files returned have a literal '*' in them. Did you mean "stop globbing on the host "? – Tim Bird Sep 7 '16 at 19:38
1

NOTE: Solution offered below tested and known to work correctly.

Single quotes will break variable expansion if you've aliased part of the path that you're globbing.

Here's a script that uses DOUBLE quotes for the use-case of the globbed filename command-configure_.tar.gz* using a path aliased with a variable:

#!/bin/bash

USEREXECUTINGSCRIPT='admin'
SCRIPTSDIR="myscripts"

SCPUSER='terrencehoulahan'
SCPUSERPASSWD='Change-Me'
SCPHOST='1.2.3.4'
SCPDIRECTORYREMOTE='/Users/terrencehoulahan/Downloads/Ubuntu/18.04'

## Uncomment appropriate below command for your distro to install "sshpass"
#apt-get -y sshpass
#yum -y install sshpass

sshpass -p "$SCPUSERPASSWD" scp -o StrictHostKeyChecking=no $SCPUSER@$SCPHOST:"$SCPDIRECTORYREMOTE/command-configure_*.tar.gz" $(getent passwd|grep ^$(echo $USEREXECUTINGSCRIPT)|cut -d ':' -f6)/$SCRIPTSDIR/

After the closing double quote, the local directory target is constructed by just extracting the specified user's home dir from /etc/passwd.

The scp command itself is prefaced with sshpass and -o StrictHostKeyChecking=no to fully automate the scp command without user interaction. Looks convoluted but better than hard-coding paths which are expected to change from time to time.

Anyhoo, an example using double quotes where variables in the path you're globbing need to be allowed to expand. HTH

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.