13

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 user.name@local.ip.num:/C:/Users/user.name/test_folder/file.html

scp /home/user.name/file.html user.name@local.ip.num:"/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 user.name@local.ip.num:"/C:/Users/user.name/test folder/file.html"

scp /home/user.name/file.html user.name@local.ip.num:"/C:/Users/user.name/test\ folder/file.html"

scp /home/user.name/file.html user.name@local.ip.num:"'/C:/Users/user.name/test\ folder/file.html'"

scp /home/user.name/file.html user.name@local.ip.num:"/C:/Users/user.name/test\\ folder/file.html"

scp /home/user.name/file.html user.name@local.ip.num:"'/C:/Users/user.name/test\\ folder/file.html'"

scp /home/user.name/file.html user.name@local.ip.num:"/C:/Users/user.name/test\\\ folder/file.html"

scp /home/user.name/file.html user.name@local.ip.num:"'/C:/Users/user.name/test\\\ folder/file.html'"

How do I get this to work?

Clay
  • 621

7 Answers7

11

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" .
10

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"'
Samuel
  • 211
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 'user.name@local.ip.num:/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 'user.name@local.ip.num:/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 :

      "'user.name@local.ip.num:/C:/Users/user.name/test folder/'"
      

      OR

    2. Escape spaces and quote complete argument like this :

      'user.name@local.ip.num:/C:/Users/user.name/test\ folder/'
      

      OR

    3. Escape twice directly using escape sequence like this

      user.name@local.ip.num:/C:/Users/user.name/test\\ folder/
      

Feel free to add-in more details.

C0deDaedalus
  • 2,730
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
Max Dax
  • 69
  • 1
  • 7
1

local linux -> remote windows

scp file.html 'user.name@windows.ip:"/C:/Users/user.name/test folder/file.html"'

remote windows -> local linux

scp -T 'user.name@windows.ip:"/C:/Users/user.name/test folder/file.html"' file.html

local windows -> remote windows

  • cmd
    scp file.html "user.name@remote.ip:\"/C:/Users/user.name/test folder/file.html\""
    
  • powershell
    scp .\file.html `"user.name@remote.ip:\`"/C:/Users/user.name/test folder/file.html\`"`"
    

remote windows -> local windows

  • cmd
    scp -T "user.name@remote.ip:\"/C:/Users/user.name/test folder/file.html\"" file.html
    
  • powershell
    scp -T `"user.name@remote.ip:\`"/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.
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.

Toto
  • 19,304