SCP and filename spaces

An issue that pops up for me occasionally is that scp doesn’t much like copying filenames that contain spaces. When you use the scp command, even in quotes – the result is that scp treats each space as a separate file.

Thankfully, there is a way to easily fix this. You need to enclose the filename in two sets of quotes. I’m guessing that one is for scp itself, and the other is for the target machine to resolve:

<code>scp user@remote-pc:"'/home/user/my long filename'" ./</code>

Notice that the path is wrapped in single quotes, and then the single quotes wrapped in double quotes. This seems to resolve the issue nicely, and still works with directories as well.