I'm trying to scp a file from my local linux machine to a remote Windows machine, and I'm coming up with some inconsistencies in how scp handles Windows file paths with spaces...

This works, note that spaces are properly escaped in the path to the local file:

scp /home/will/file\ with\ spaces.txt remote@host:D:/Users/will/Downloads/

However, this does not work, despite the space in the "Google Drive" folder being properly escaped:

scp /home/will/file\ with\ spaces.txt remote@host:D:/Users/will/Google\\ Drive/Documents/Computer_Stuff/Home_Lab/folder

I've gone through a ton of resources online where people have similar problems, and all their solutions didn't work for me.

I tried putting the Windows file path in double quotes, single quotes, both with and without escaping spaces; I've tried using double \\ and triple \\\ to escape spaces; I even tried escaping the colon (D\:); and I tried explicitly stating a target filename and not. Nothing worked.

Then I found this answer, and only method #2 works! Why? What is the difference between surrounding the Windows file path in '", and "'? Why can I simply escape spaces without using any quotes at all in the local Linux file path, but not in the remote Windows path?

  • Why don't you just single quote the paths? 'remote@host:D:/Users/will/Google Drive/Documents/...'
    – Kusalananda
    May 11 '18 at 15:02
  • since the remote host is a Windows Maschine i would try to use a Windows Style formatted/escaped Path like this D:\Users\will\Google^ Drive\Documents\Computer_Stuff\Home_Lab\folder
    – konqui
    May 11 '18 at 15:15
  • Windows has long been agnostic as to whether to use forward slashes or backslashes to delimit directories within paths.
    – DopeGhoti
    May 11 '18 at 15:18

I was going crazy on Windows 10 -- this worked for me

scp -r user@hostname:"/Library/Application' 'Support/Adobe/Common/Plug-ins/7.0/" ./Adobe
  • THANK YOU 🙌🙌🙌
    – user1234
    Jul 18 at 20:36

As mentioned in the question and answered here, method 2 was the solution that worked in my case. Adding as an answer for convenience of later readers.

  • Windows 10
  • Git bash scp

Single / Double Quotes:
scp ./myfile.txt user@host:'"/c:/AppName/a poorly named dir/sub_dir/myfile.txt"'

  • OMFG!!!! Single-double-quote!!!! This magic just saved my sanity!
    – cybertoast
    Nov 23 at 13:45

Use the -T option:

scp -T user@remote.host:'"c:\path with\spaces in\it\foo.txt"' .

Your shell is seeing Google\\ Drive, and parsing it as Google, followed by a literal backslash, a space (which separates the arguments to scp, followed by Drive, and then passing that on to SCP which doesn't know what to do now that you're sending it too many arguments.

To make this more clear, compare how the shell parses Google\ Drive and Google\\ Drive:

$ cat 443236.sh
foo=(Google\ Drive)
echo "Unrolling \$foo[@]:"
for f in "${foo[@]}"; do
  echo "'$f'"
foo=(Google\\ Drive)
echo "Unrolling \$foo[@]:"
for f in "${foo[@]}"; do
  echo "'$f'"
$ ./443236.sh
Unrolling $foo[@]:
'Google Drive'
Unrolling $foo[@]:
  • 1
    Could someone advise the answer, e.g. what goes in an scp statement.
    – NZ Dev
    Oct 22 '18 at 8:06
  • The two required arguments for an scp invocation are the source and destination for the file to be copied, e. g. scp /path/to/some/file user@host:/location/for/the/file or scp user@host:relative/path/to/file ./directory/.
    – DopeGhoti
    Oct 22 '18 at 15:39
  • That doesn't advise the solution to fixing the above issue -->" scp /home/will/file\ with\ spaces.txt remote@host:D:/Users/will/Google\\ Drive/Documents/Computer_Stuff/Home_Lab/folder"
    – NZ Dev
    Oct 24 '18 at 4:40
  • That is already resolved in the actual answer.
    – DopeGhoti
    Oct 24 '18 at 15:09
  • An easier illustration of unrolling is printf '<%s>\n' whatever, e.g. printf '<%s>\n' Google\ Drive; printf '<%s>\n' Google\\ Drive
    – Wildcard
    Jul 9 '19 at 23:25

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.