I've been playing around with SCP for a while, and I've been able to transfer files from one place to another. It all worked nicely until I needed to copy a file from Ubuntu to a Windows user who had spaces in his username (* groan *)

While I got it working for SSH like this:

ssh "User Name Spaces"@

It didn't work with SCP:

scp "myfile.txt" "User Name Spaces"@

As it returns:

User Name Spaces: invalid user name

I've tried all kinds of things:

scp "myfile.txt" "User Name Spaces"@
scp "myfile.txt" "User\ Name\ Spaces"@
scp "myfile.txt" "User\\ Name\\ Spaces"@
scp "myfile.txt" "User\\\ Name\\\ Spaces"@
scp "myfile.txt" "'User Name Spaces'"@
scp "myfile.txt" "'User\ Name\ Spaces'"@
scp "myfile.txt" "'User\\ Name\\ Spaces'"@
scp "myfile.txt" "'User\\\ Name\\\ Spaces'"@
scp "myfile.txt" "'User Name Spaces'@":folder

And so on... but nothing seems to work

Does anyone have any ideas?

  • 1
    Take a look at this link. stackoverflow.com/questions/9425222/… It looks like you should be able to replace the spaces with underscores. Have you tried that already?
    – Andrew
    May 19, 2016 at 23:27
  • Yup, I did try to use underscores instead of spaces (and no quotes), and although it doesn't raise an error I guess it treats it as a different user because when I use the correct password, it fails with "Permission Denied".
    – cavpollo
    May 19, 2016 at 23:36

2 Answers 2


You could use ~/.ssh/config. It's the per-user configuration file for SSH (and commands based on SSH like scp and sftp). Create it, if it does not exist, and add:

    User "User Name Spaces"

For example:

Host localhost
    User "foo bar"


$ scp test.txt  localhost:
foo bar@localhost's password: 

(I don't actually have a user named foo bar, so I couldn't finish the test.)

You could also create an alias in .ssh/config:

Host foo
    User "User Name Spaces"

Then you can conveniently do:

ssh foo
scp file foo:

And avoid having to type the whole IP address.

An alternative, if you have tar available on both ends:

tar c myfile.txt | ssh "User Name Spaces"@ tar x

tar creates a tar archive, saved to standard output and piped through to SSH, where it becomes the input to tar x, which extracts the archive. You can use options like -C to change the directory (or do so as a shell command before tar: ssh … 'cd foo; tar x). You could also use compression and save a bit on network bandwitdh:

tar zc myfile.txt | ssh "User Name Spaces"@ tar zx
  • I didn't understand exactly how tar and the pipe character work together with ssh, but it sounds like a good idea. Sadly, while testing it, it says something along the lines of "tar isnt recognized as an internal or external command" [Note: tar does work on my side, Ubuntu. I guess it comes from the Windows PC not knowing what to do with the command.]
    – cavpollo
    May 19, 2016 at 23:45
  • Hmm. I assumed you might have installed those utilities along with SSH. No hope of installing tar on the Windows system (you can get it from MSYS2 or Cygwin)?
    – muru
    May 19, 2016 at 23:48
  • No, unfortunetly the windows pc is out of my control =\ (The ssh server is running thanks "Bitvise SSH Server")
    – cavpollo
    May 19, 2016 at 23:53
  • @cavpollo try the update
    – muru
    May 20, 2016 at 0:04
  • 1
    @cavpollo Yes, it's the same way. I hadn't noticed the comment. OSX uses OpenSSH, the same implementation Ubuntu uses, so it's applicable here too.
    – muru
    May 20, 2016 at 0:16

I find the -o user= option to be more reliable. It works with spaces and apostrophes. Good to use if the username will be somewhat dynamic.

For example:

  • scp -o user="Your Username" ~/file remote:~/file
  • scp -o user="O'Neill" remote:~/file ~/file
  • scp -o user="${USERNAME_FROM_VARIABLE}" remote:~/file ~/file

Source: https://serverfault.com/questions/955058/scp-with-special-username

You must log in to answer this question.

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