11

To push a file from a Linux terminal to a Windows system, the following two examples work just fine.

scp /home/user.name/file.html [email protected]:/C:/Users/user.name/test_folder/file.html

scp /home/user.name/file.html [email protected]:"/C:/Users/user.name/test_folder/file.html"

I need to do this where the local folder has spaces and I cannot change the name, say /C:/Users/user.name/test folder/

All of the following fail with the message scp: ambiguous target

scp /home/user.name/file.html [email protected]:"/C:/Users/user.name/test folder/file.html"

scp /home/user.name/file.html [email protected]:"/C:/Users/user.name/test\ folder/file.html"

scp /home/user.name/file.html [email protected]:"'/C:/Users/user.name/test\ folder/file.html'"

scp /home/user.name/file.html [email protected]:"/C:/Users/user.name/test\\ folder/file.html"

scp /home/user.name/file.html [email protected]:"'/C:/Users/user.name/test\\ folder/file.html'"

scp /home/user.name/file.html [email protected]:"/C:/Users/user.name/test\\\ folder/file.html"

scp /home/user.name/file.html [email protected]:"'/C:/Users/user.name/test\\\ folder/file.html'"

How do I get this to work?

2
  • 1
    First thing I'd try is to quote the complete argument (because it is a single argument): scp /home/user.name/file.html '[email protected]:/C:/Users/user.name/test folder/'. If that doesn't work, next thing is to read scp source code to find out how it parses this argument.
    – dirkt
    Commented Apr 4, 2018 at 17:22
  • Worked perfectly!
    – Clay
    Commented Apr 4, 2018 at 21:05

7 Answers 7

7
  • Try using quotes ' or Double quotes " around complete argument.

    As suggested by @dirkt in comments Quoting the complete path argument for the ssh host should do the work. That makes your command like this :

    scp /home/user.name/file.html '[email protected]:/C:/Users/user.name/test folder/'
    
  • Use escape sequence for space in between name of folder.

    You can use \ (backslash with a space) that is escape sequence for a space. That makes your command like this :

    scp /home/user.name/file.html '[email protected]:/C:/Users/user.name/test\ folder/'
    

    Notice the \ with a space in between test & folder that makes it test\ folder.

  • It maybe the case that you need to escape twice as It is escaped first locally and then on the remote end. In that case you can write like this :

    1. "'complete argument'" inside quotes within double quotes like this :

      "'[email protected]:/C:/Users/user.name/test folder/'"
      

      OR

    2. Escape spaces and quote complete argument like this :

      '[email protected]:/C:/Users/user.name/test\ folder/'
      

      OR

    3. Escape twice directly using escape sequence like this

      [email protected]:/C:/Users/user.name/test\\ folder/
      

Feel free to add-in more details.

2
  • 2
    Now that there is a native OpenSSH in Windows 10, adding backslashes to it won't work. Commented Apr 7, 2019 at 15:50
  • 1
    first option worked for me Commented Sep 12, 2021 at 17:15
9

Tried all the options above to scp from CentOS client to Windows 7 (SP1), but without success.

This one worked for me though:

$ scp /tmp/lala 'win7@<IP>:"/Users/win7/Documents/TestComplete 14 Projects"'
1
  • 4
    This should have a lot more upvotes since it's the only method I found working while trying to copy files from a Ubuntu machine to the built-in OpenSSH version on Windows 10. Works with spaces in the Windows path as well as special characters (x86).
    – adiuva
    Commented May 17, 2020 at 13:22
7

I don't know if this is still relevant, but one solution (that might backfire in some situations) is using "?" instead of space:

scp "user@ip:/home/user/file?with?spaces.txt" .
2
  • 2
    This worked for me using SCP from Windows command prompt and remote machine being Ubuntu server. Commented Nov 12, 2022 at 0:22
  • For some reason, that was the only thing working in my case. PowerShell kept splitting my path as if it were mutiple arguments...
    – Adrien H
    Commented Jun 25 at 13:59
2

This worked for me for copying a file from Ubuntu 22.04 Linux into Windows 10 (Note the command syntax was executed without SSH login and just literally running the code from windows command prompt):

scp -r "user_name@remote_IP_address:Downloads/'This is some text file'" C:\Users\Admin\Downloads
1
  • took me a long time to scroll down here, but finally the one that worked for me :) Commented Jun 12, 2023 at 13:19
0

The " symbol is interpreted by bash itself. Bash decapsulates anything in between interbretes if it can and just presents the outout to a command being called as a string that may have some special symbols. Try using the apostrophe (') symbol for it forces bash not to look at the string at all and just pass it to a command that was invoked first. Those strings with ' should be formatted like this: If you want to see

don't

echo $'dont\'t'
0

Well, one more possible solution is just use Git Bash or any other bash emulator.

Then just escape all spaces with \ before each and wrap statemens with double quotes.

0

local linux -> remote windows

scp file.html '[email protected]:"/C:/Users/user.name/test folder/file.html"'

remote windows -> local linux

scp -T '[email protected]:"/C:/Users/user.name/test folder/file.html"' file.html

local windows -> remote windows

  • cmd
    scp file.html "[email protected]:\"/C:/Users/user.name/test folder/file.html\""
    
  • powershell
    scp .\file.html `"[email protected]:\`"/C:/Users/user.name/test folder/file.html\`"`"
    

remote windows -> local windows

  • cmd
    scp -T "[email protected]:\"/C:/Users/user.name/test folder/file.html\"" file.html
    
  • powershell
    scp -T `"[email protected]:\`"/C:/Users/user.name/test folder/file.html\`"`" .\file.html
    

the -T option

$ man scp | grep -A 3 -- -T
     -T      Disable strict filename checking.  By default when copying files from a remote host to a local directory scp checks that the received file‐
             names match those requested on the command-line to prevent the remote end from sending unexpected or unwanted files.  Because of differences
             in how various operating systems and shells interpret filename wildcards, these checks may cause wanted files to be rejected.  This option
             disables these checks at the expense of fully trusting that the server will not send unexpected filenames.

You must log in to answer this question.

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