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
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