regex/wildcard in scp
I keep on getting «scp: ambiguous target» Actually I am calling an api with source and dest that uses scp underneath and loops over diff hosts to put files Thanks!
1 Answer 1
In general, yes, it is certainly possible to use a wildcard in scp.
But, in your scp command, the second argument is the target, the first argument is the source. You certainly cannot copy a source into multiple targets.
If you were trying to copy multiple jars, for example, then the following would certainly work:
scp path/*.jar user@host:path2/jar/
«ambigious target» in this case is specifically complaining that the wildcard you’re using results in multiple possible target directories on the @host system.
If you want to copy to multiple directories on a remote system and have to determine them dynamically, a script like the following should work:
dir_list=$(ssh user@host ls -d '/path1/foo*/path2/jar/') for dir in $dir_list; do scp path/file.jar user@host:$dir done
The dir_list variable will hold the results of the execution of the ls on the remote system. The -d is so that you get the directory names, not their contents. The single quotes are to ensure that wildcard expansion waits to execute on the remote system, not on the local system.
And then you’ll loop through each dir to do the remote copy into that directory.
(All this is ksh syntax, btw.)
scp copy has error «ambiguous target»
I try to copy files from a linux (ubuntu) machine to an external hard drive mounted on a mac but got an error message :
What I did is, I’m on a mac, ssh to the linux machine where files are. Then use the following command :
scp fileToCopy myMacUser@myMacMachine:/Volumes/MyExternalDrive/targetDirectory
Happened to me when I had an extra param ( -t ; a remnant from a previous ssh command) in the arg list; apparently it is not supported by scp but the error I got was ambiguous target 🙁
3 Answers 3
If you have white space in a path, you have to escape the characters by using double backslashes \\ and enclosing the entire path in quotes:
scp myfile.txt user@192.168.1.100:"/file\\ path\\ with\\ spaces/myfile.txt"
yes, that is it ! I first tried with double \\ but didn’t work and then I tried adding » » around my path with \\. That does the job. Thanks.
Double quotes in single qoutes without escaping spaces also work ( scp myfile.txt user@192.168.1.100:'»/file path with spaces/myfile.txt»‘ ). In fact you must escape the filename twice: first time from the local shell, and second time from the remote one.
I found that two sets of quotes worked for me, around the target location. Double quotes outside, and single quotes inside the double quotes. For example:
scp "my local file.txt" user@192.168.1.100:"'/folder/my spacey folder name/'"
The outside set of quotes are for the local shell, and the inside quotes are for the remote shell. Thanks to @mik for the suggestion in comments.
Scp ambiguous target linux
scp: ambiguous target
If you are transferring files from your host to another host and there are spaces in the path, you could get this error.
If there are spaces in the directory name of source (your system) and you have used double quotes in the path, you will not get the error but if there are spaces in the directory name of destination (remote host) and you have used double quotes in the path, you could still get the error.
To remove this error
Use backslash before spaces.
Suppose there is a directory «linux host» in the root of Remote Host and you want to transfer a file test.txt from your root directory to the remote host directory «linux host». You should use this command.
# scp "/root/test.txt" 192.168.xx.xx:"/root/linux\ host"
Since there is space in the directory name «linux host» that’s why I have used backslash before space.
Now you will not get the error while file transfer.
If you put just double quotes in the remote host path and you do not put backslash before space, you will get the error scp: ambiguous target
One more example :
# scp "/root/Documents/file.txt" 192.168.xx.xx:"/root/untitled\ folder/red\ hat/linux\ host"
Linux Utility — Scp (secure copy, remote file copy) command
You may be able to pass the password with the PWD environment variable.
One file
The authenticity of host 'MyDestinationHost(192.168.0.1)' can't be established. RSA key fingerprint is 45:b8:dc:fd:22:da:73:eb:db:e3:fb:89:7e:c8:0a:6e. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'MyDestinationHost,192.168.0.1' (RSA) to the list of known hosts. [email protected]'s password: SAF_OR_10M_T.dmp 100% 3555MB 7.0MB/s 08:30
Support
scp: ambiguous target caused by space in path
The below scp command will get you a scp: ambiguous target error message because of the spaces in the last argument.
To resolve this issue, just quote the whole path of the last argument without forgetting the escape character.
Linux file management See Using Parameters Expansion Removal From a path string where the file does not exist dirname returns the first parent of an existing path file. .
Winscp is a windows file manager that can make a ssh connection and use the SCP protocol. Just fill the needed information as below :
How to fix scp: Ambiguous Target Error Solved
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.
Filed Under: misc, network, ubuntu Tagged With: scp, ubuntu