January 13, 2004

Newbie Unix tip: I don't normally do Unix tips (though there's no reason why I shouldn't), but I've tripped over this enough times that I should probably blog it to reduce the likelihood of tripping again later. scp lets you securely copy files between hosts over an ssh connection. When specifying remote filenames or directories that have characters that require shell escaping, such as names that contain spaces, you have to escape the characters twice: once for your local shell, and once for the remote shell.

  scp "Source File" "user@remotehost.com:~/Destination File"

will result in:

  scp: ambiguous target

because, despite the correct use of quotes to tell the shell that "...~/Destination File" is all one term, scp interprets the destination filename as two terms, "~/Destination" and "File". Additionally escaping the spaces in the remote filename within the quotes works as originally intended:

  scp "Source File" "user@remotehost.com:~/Destination\ File"

Notice that local names (like "Source File") do not need to be double-escaped. [Update: fixed example.]

comments...

thanks for posting this. I found it on Google, cleared me up right away. It's a non-intuitive way of doing it, if you ask me, but I guess it makes sense...

HA - me to. I just tried a gazillion other ways of trying to escape the space, but I shuld have double-escaped it...



Thanx to you as well as Google that also helped me localize the mistake/solution

I encountered the same error .. but had a different cause.

My scp was
scp -t -C user@server:file1 file2

the -t, because it is not a valid switch for scp (it is for ssh) was getting caught...

It was late, but might help someone else buring the midnight oil...

Make me the third. You've made a tired NetAdmin happy!