How to SCP a path with spaces

Baby Tux

I recently needed to remotely copy over SSH a folder from the remote machine to my local machine. Usually this is not a problem, however the path to this folder had a space in it. The folder itself is rather large and contains files which wouldn't really benefit from compression. It turns out the solution was quite simple.

[adsense:468x60:4496506397]

These instructions are assuming you're connecting from a unix-like system to a unix-like system (eg Linux or OSX).

scp -r myserver.com:"/path/with/a/Space\\ In\\ It" ./

This basically says "recursively copy the folder at myserver.com (using the local username) to the current folder using scp". Now the spaces need to be double escaped as the first escape only signifies that they're spaces on the LOCAL machine - by the time they got to the remote machine, they wouldn't be escaped anymore. By double escaping them locally, by the time the path makes it to the remote machine they are just single escaped. I have to admit - I'm not 100% sure why you need the double quotes around the path itself.

Personally, I tend to run the scp command within a separate screen so I can detach it and reattach it later. It also means that if my connection from my machine to the machine I'm SSH'd to (be it local or remote) drops or crashes, the process will continue to run and I can reattach the screen when I reconnect.

Comment Icon

27 Comments

The most recent comment was on Mon, 2nd Jan 2012 - 12:44

Really appreciate stuff like this. Thank you very much

Great but i am not sure if you need to escape the slashes.
But if it is then Thanks for the post.

This also worked for me, and it's easier if you have a filename with a lot of spaces in it:

scp user@host:"'file name with lots of spaces'"

Your local shell will ignore the outer double quotes and the remote shell will receive the filename in single quotes. You would probably have to do something else if the remote filename has " or ' characters.

this worked for me. I had a file with spaces in it.

t.

thanks for this tip
i already tried with only double quotes and only \\ but didn't succeed and had always to resolve to symlink the wanted file and scp the symlink

Thanks a bunch, this really helped me out!

Thanks,this is the only infomation I found about this problem.

Thank you!

May the google follow thine link everytime I need it :)

I was about to change my file structure and remove all spaces when I found your post. This double escaping stuff is always tricky :)

Thanks a lot for putting this up.

Abhaya

The reason why you needed quotes around the remote path is because you've escaped your escape - using \\ escapes the backslash, leaving the following space unescaped. So yet another alternative is to use:

scp -r myserver.com:/path/with/a/Space\\\ In\\\ It ./

Note the three backslashes - two to produce a single escaped backslash (which is presumably sent to the remote host) and a third to escape the following space locally. Phew!

I had to TRIPLE escape the slash when I didn't use quotes around the entire path.

scp server:~/This\\\ file\\\ please.txt .

Apparently this tells scp to send a backslash (escaping the backslash) and a space (escaping the space) to the host system.

-Aaron-

Thank you!

The bottom line is that a remote pathname must be passed by the local shell to scp as quoted, because the pathname is interpreted on the remote end. To protect the quoting from the local shell, a second level of quoting is required. Any of these will work (at least in bash):

scp -r myserver.com:'"/path/with/a/Space In It"' ./
scp -r myserver.com:"'/path/with/a/Space In It'" ./
scp -r myserver.com:'/path/with/a/Space\ In\ It' ./
scp -r myserver.com:"/path/with/a/Space\ In\ It" ./
scp -r myserver.com:/path/with/a/Space\\\ In\\\ It ./

Your stated solution is really the fourth one above - the \\ in your solution resolves to \ because \ retains its special meaning inside double quotes when it is followed by a \. So only \ is necessary.

Thanks!

Thanks for the info

Really thanks !!!!

Add new comment

Filtered HTML

  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <img> <p>
  • You can use BBCode tags in the text. URLs will automatically be converted to links.
  • You can enable syntax highlighting of source code with the following tags: <code>, <pre>, <bash>, <css>, <html>, <js>, <jquery>, <mysql>, <php>. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.