0

From my bash shell, I am able to run this following command successfully - scp <local-folder>/* <user>@<remote-host>:/var/tmp. All files in my <local-folder> are copied onto the remote location.

Now I am trying to run the above same command from my Go program, with exec.Command() but scp complains of "no such file / directory - <local-folder>/*' - the '*' is literally taken as a filename. I want to replicate the same behaviour of scp I get when I run it from my Bash shell. Here is the code snippet I am using:

    pushCmd := exec.Command("scp", "<local-folder>/*", "<user>@<remote-host>:/var/tmp")
    pushCmdOutput, err = pushCmd.CombinedOutput()
    fmt.Fprintln(os.Stderr, string(pushCmdOutput))

Thanks in advance.

Srivathsan M
  • 87
  • 1
  • 9
  • 2
    Use `bash -c`, like here https://stackoverflow.com/a/30329351/965900 (it answers a problem with pipes, but just like the pipe, the glob pattern is a thing of the shell) – mkopriva Jan 03 '22 at 15:10
  • 3
    If you want `bash` to expand the `*`, then you need to execute `bash` (or simulate what it's doing yourself) – JimB Jan 03 '22 at 15:10
  • Aaargh! Yes; that bash expands such glob patterns first, is something I missed. Thanks @mkopriva and JimB – Srivathsan M Jan 03 '22 at 15:14
  • @SrivathsanM, just to complement the answers given, look at this complete example: https://go.dev/play/p/1cD9WamklgN. – Ronaldo Lanhellas Jan 03 '22 at 15:43

0 Answers0