How do I escape spaces in path for scp copy in Linux?

后端 未结 6 618
抹茶落季
抹茶落季 2020-11-30 17:13

I\'m new to linux, I want to copy a file from remote to local system... now I\'m using scp command in linux system.. I have some folders or files names are with spaces, when

相关标签:
6条回答
  • 2020-11-30 17:26

    works

    scp localhost:"f/a\ b\ c" .
    
    scp localhost:'f/a\ b\ c' .
    

    does not work

    scp localhost:'f/a b c' .
    

    The reason is that the string is interpreted by the shell before the path is passed to the scp command. So when it gets to the remote the remote is looking for a string with unescaped quotes and it fails

    To see this in action, start a shell with the -vx options ie bash -vx and it will display the interpolated version of the command as it runs it.

    0 讨论(0)
  • 2020-11-30 17:29

    Use 3 backslashes to escape spaces in names of directories:

    scp user@host:/path/to/directory\\\ with\\\ spaces/file ~/Downloads

    should copy to your Downloads directory the file from the remote directory called directory with spaces.

    0 讨论(0)
  • 2020-11-30 17:30

    Sorry for using this Linux question to put this tip for Powershell on Windows 10: the space char escaping with backslashes or surrounding with quotes didn't work for me in this case. Not efficient, but I solved it using the "?" char instead:

    for the file "tasks.txt Jun-22.bkp" I downloaded it using "tasks.txt?Jun-22.bkp"

    0 讨论(0)
  • 2020-11-30 17:34

    I had huge difficulty getting this to work for a shell variable containing a filename with whitespace. For some reason using:

    file="foo bar/baz"
    scp user@example.com:"'$file'"
    

    as in @Adrian's answer seems to fail.

    Turns out that what works best is using a parameter expansion to prepend backslashes to the whitespace as follows:

    file="foo bar/baz"
    file=${file// /\\ }
    scp user@example.com:"$file"
    
    0 讨论(0)
  • 2020-11-30 17:43

    Basically you need to escape it twice, because it's escaped locally and then on the remote end.

    There are a couple of options you can do (in bash):

    scp user@example.com:"'web/tmp/Master File 18 10 13.xls'" .
    scp user@example.com:"web/tmp/Master\ File\ 18\ 10\ 13.xls" .
    scp user@example.com:web/tmp/Master\\\ File\\\ 18\\\ 10\\\ 13.xls .
    
    0 讨论(0)
  • 2020-11-30 17:44

    Also you can do something like:

    scp foo@bar:"\"apath/with spaces in it/\""
    

    The first level of quotes will be interpreted by scp and then the second level of quotes will preserve the spaces.

    0 讨论(0)
提交回复
热议问题