regex/wildcard in scp
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.
--- EDIT:
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.)
codeObserver
Updated on June 04, 2022Comments
-
codeObserver almost 2 years
Is it possible to use a wildcard in scp
I am trying to achieve:
loop { substitue_host (scp path/file.jar user@host:path1/foo*/path2/jar/) }
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!