Scp a file with spaces or parentheses in the name

I have googled this so many times I thought I would put it down for myself. I found this great blurb on Tony Arkles blog.

Here’s a stupid unix trick: scp is an awesome tool for moving files around between unix-y machines (for instance, my Mac laptop and my Linux HTPC). Often enough, though, I need to copy a file with a space in the name, and this stumped me for a while.

Here’s the answer.  You need to quote it twice:

scp username@"'my file has spaces in it'" .

The first set of quotes protects the second set of quotes from the shell, so the file name gets passed over ssh with the quotes still in it… then the shell on the other end sees the quotes and removes them (preserving the spaces).

One Comment

  1. I’ve been struggling for a while with spaces and brackets in a filename when using scp. Excellent find. Thanks. I may pinch this for my site and link back

Leave a Reply

Your email address will not be published.