8

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?

3
  • Why don't you just single quote the paths? 'remote@host:D:/Users/will/Google Drive/Documents/...'
    – Kusalananda
    Commented May 11, 2018 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
    Commented May 11, 2018 at 15:15
  • 1
    Windows has long been agnostic as to whether to use forward slashes or backslashes to delimit directories within paths.
    – DopeGhoti
    Commented May 11, 2018 at 15:18

5 Answers 5

8

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

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

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

Use the -T option:

scp -T [email protected]:'"c:\path with\spaces in\it\foo.txt"' .
0

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
#!/bin/bash
foo=(Google\ Drive)
echo "Unrolling \$foo[@]:"
for f in "${foo[@]}"; do
  echo "'$f'"
done
foo=(Google\\ Drive)
echo "Unrolling \$foo[@]:"
for f in "${foo[@]}"; do
  echo "'$f'"
done
$ ./443236.sh
Unrolling $foo[@]:
'Google Drive'
Unrolling $foo[@]:
'Google\'
'Drive'
5
  • 1
    Could someone advise the answer, e.g. what goes in an scp statement. Commented Oct 22, 2018 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
    Commented Oct 22, 2018 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" Commented Oct 24, 2018 at 4:40
  • That is already resolved in the actual answer.
    – DopeGhoti
    Commented Oct 24, 2018 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
    Commented Jul 9, 2019 at 23:25
0

Platform:

  • scp on Ubuntu 18.04 ARM
  • OpenSSH server on Windows 10

Single / Double Quotes in the above answer will work, but I want to use a directory variable. After some searches, the following works.

If you want to use a variable in the script:

sshpass -p "$password" scp myfile.txt user@host:'"'"$directory"'"'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .