5

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?

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • Try using double quotes instead: scp "macbook@192.168.0.3:/Users/macbook/desktop/John's folder/file storage/folder". But also just don't give files/directories such crazy names. – jesse_b Apr 19 '18 at 18:23

3 Answers3

4

As of OpenSSH version 9.0 released on 2022-04-08, the scp program now uses the SFTP protocol directly. As a consequence the original issue no longer exists, and therefore the remainder of this answer is no longer required.

If you are using a legacy version of scp, or you want to revert to the old SCP protocol with scp -O, the answer still applies.

Original answer follows.


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
Chris Davies
  • 116,213
  • 16
  • 160
  • 287
0

SCP needs to include one entire parameter in quotations. 2 different answers before me are partially right, but the correct answer is

scp "macbook@192.168.0.3:/Users/macbook/desktop/John's folder/file" storage/folder

Notice that the first parameter is wrapped in quotes, but not the second - if both were wrapped in a single instance of quotations as one comments suggests, the /bin/scp would invoke this as a single parameter, and be expecting another parameter after that.

scp "macbook@192.168.0.3:/Users/macbook/desktop/John's folder/file storage/folder" will be read as a single parameter. Even though there is a space there, the double quotes escape that.

Finally, putting quotations in the middle of a parameter like scp macbook@192.168.0.3:"/Users/macbook/desktop/John's folder/file" storage/folder cuts the paarameter in half and won't make sense to the shell because user@host:/path/to/directory is one full parameter. You can use parts of it, but you cannot cut it in half or it will be examined as 2 different params.

SomeGuy
  • 242
  • Changing quotes within a token (aka word) works fine in any POSIX shell. Example (in bash): foo=bar; echo fine'$foo'more"$foo" -> fine$foomorebar – dave_thompson_085 Apr 20 '18 at 07:35
0

Triple escape works for me:

scp macbook@192.168.0.3:bob\\\'s\\\ file /local/path/

These also work:

scp "macbook@192.168.0.3:bob\'s\ file" /local/path/
scp macbook@192.168.0.3:"bob\'s\ file" /local/path/

These do not:

scp "macbook@192.168.0.3:bob's file" /local/path/
scp macbook@192.168.0.3:bob\'s\ file /local/path/