Wednesday, May 6, 2009

scp: ambiguous target

I was attempting to secure copy a file over a remote server and I encountered the following error:
$ scp file.txt user@ip_address:"/file path/"
scp: ambiguous target

So after some trial and error I discovered the problem was the space " " in the path to which I was attempting to copy the file to (despite having put the path within quotes).

To solve this problem you need to escape the space e.g.
$ scp file.txt user@ip_address:"/file\ path/"

19 comments:

  1. thanks, exactly what i was looking for. Worked perfectly!

    ReplyDelete
  2. tks a lot ... it just helped me with my mac and my hacked ATV...

    Great tip..

    ReplyDelete
  3. Thanks a lot! I have been fighting this for a couple of days. This is the best explanation I have found, and yes it worked!

    ReplyDelete
  4. I've been pulling my hair out over this, thanks man

    ReplyDelete
  5. You will also get this error if you just escape spaces and DON'T quote.

    So you must quote and escape spaces.

    ReplyDelete
  6. I tried to scp files from cygwin to linux host via command:
    #ssh CYGWIN_HOST 'scp /root/test.txt root@LINUX_HOST:/root/test.txt'
    scp: command error: Ambiguous target file.

    It was fixed by:
    #ssh CYGWIN_HOST 'DEST="/root/test.txt"; scp /root/test.txt root@LINUX_HOST:/root/test.txt'

    ReplyDelete
  7. Hi, since this is in the top Google results, I thought I'd share that a more elegant way might be to add escaped nested quotes. Example:

    scp documents.zip remote:"\"/var/path/containing some spaces/foo/\""

    ReplyDelete
  8. Thanks so much! I had escaped the spaces as you describe, but I had not used the quotes!! Such a life saver, man!

    ReplyDelete
  9. Thank you for the solution that helped even 7 years later :)

    ReplyDelete
  10. I've been stuck on this for quite a while... thanks a lot man!

    ReplyDelete
  11. Thanks much ...It working perfectly

    ReplyDelete
  12. Thanks much....It is working properly

    ReplyDelete
  13. 4 Years later and this post has just saved me so much frustration. THANK YOU.

    ReplyDelete
  14. Great, thanks for this. Helped me out!

    ReplyDelete
  15. This solved the problem instantly. Thank you.

    ReplyDelete
  16. I am trying the same but when i exit my putty session or putty session gets closed due to timeout, it stops scping. Also i do not see any jobs once i re-login. But if i do "ps -ef|grep scp" i can see the scp process there but it is hung as it is not scping anything.

    Any suggestion in this case please ?
    linux scp

    ReplyDelete
    Replies
    1. The process will stop as soon as you exit putty. You will probably need to use nohup e.g. 'nohup [scp command] &'. Not really sure how your grep command returns the process (unless it is the grep command itself that it is finding?).

      Delete