SCP Command – Troubleshooting File Copy Errors

scp

I use scp to transfer files from my android to my MacBook which works like a charm. But I have a folder called John's folder on my MacBook so when I attempt to copy a file inside that directory like
scp macbook@192.168.0.3:/Users/macbook/desktop/John\'s\ folder/file storage/folder

It throws back an error

unexpected EOF error while looking for matching \`’\`

and

unexpected end of file

How do I resolve this?

Best Answer

This is interesting. The other answers that I'm seeing are telling you to swap your escaped quote and escaped space for a quoted string. Actually they're equivalent so you'll see no change (a\'\ b is the same to the shell as "a' b").

The problem here lies in the way that scp on the remote system interprets the command line that it's being given.

As an example, this would work:

scp John\'s\ folder/file localhost:/tmp/dst

But this would fail:

scp localhost:/tmp/src/John\'s\ folder/file /tmp/dst

(I've used localhost for the example; you should use user@host for your situation.)

If you include the -v (verbose) flag on scp you can see exactly what's going on that gives rise to the failure:

debug1: Sending command: scp -v -f /tmp/src/John's folder/file
bash: -c: line 0: unexpected EOF while looking for matching `''
bash: -c: line 1: syntax error: unexpected end of file

The unfortunate solution here is that you need to escape special characters (including whitespace) twice - once for the local shell, and once for the remote shell:

scp localhost:"/tmp/src/John\'s\ folder/file" /tmp/dst