How can I scp when path and / or filename to copy has spaces?

I have in a remote remote_host a file like this:

\home\user\path with spaces\filename with spaces.ext

And I want to scp it to current dir .. How can I do that?

Thanks!


After some investigation, I have found these two options:

scp 'user@remote_host:"/home/user/path with spaces/filename with spaces.ext"' .

and

scp 'user@remote_host:/home/user/path\ with\ spaces/filename\ with\ spaces.ext' .

I hope it helps you too!

Have fun!

Edit - openssh was updated with a security feature that causes, with certain characters in the path name, an error message "protocol error: filename does not match request". In that case using scp's new -T option will allow it to work. for example

scp -T 'user@remote_host:"/home/user/path with spaces/filename with spaces and (2018).ext"' .

This is explained in https://stackoverflow.com/a/54599326


You have two options.

Option one: enter spaces as "\ ". Example; /home/matthew/File\ With\ Spaces.file.

Option two: enter the path in apostrophes single quotes like so: '/home/matthew/File With Spaces.file'.

If you use tabulator key (Tab) to auto-complete the file name, it will insert "\ " automatically, however you need to be using public key authentication for it to work on a remote host.