Recently I needed to copy a file using scp to another server and encountered the following error: “scp: ambiguous target” In doing some testing I figured out that the space in the file name was the problem. Here is how to handle a file name with a space in it.
I even tried putting the file name in quotes, and that didn’t take care of the ambiguous target error message. For example, I was trying to use the following command:
scp myfile.txt user@192.168.1.100:”/file path with spaces/myfile.txt”
In trying to put the quotations marks in several different places, it still didn’t work. What you need to do is escape the space with a backslash. what is meant by this is that you need to put a backslash in front of every space in the file path name. So the above command then becomes:
scp myfile.txt user@192.168.1.100:”/file\ path\ with\ spaces/myfile.txt”
This takes care of the scp: ambiguous target error message from the command line, but what about running inside a shell script. In that case, you would add two backslashes. So the above command then becomes:
scp myfile.txt user@192.168.1.100:”/file\\ path\\ with\\ spaces/myfile.txt”
This article was originally posted on www.mikestechblog.com Any reproduction on any other site is prohibited and a violation of copyright laws.