I am trying to run the find
command using exec.Command
:
cmd := exec.Command("find", "/usr/bin", "-maxdepth",
"2", "-iname", "'*go*'", "|", "head", "-10")
out, err := cmd.CombinedOutput()
fmt.Println(err)
fmt.Println(string(out))
Unfortunately this fails with the following output:
exit status 1
find: paths must precede expression: |
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
What am I missing here?
EDIT:
Even if I try it without piping this still fails:
cmd := exec.Command("find", "/usr/bin", "-maxdepth", "2", "-iname", "'*go*'")
out, err := cmd.CombinedOutput()
fmt.Println(err)
fmt.Println(string(out))
Output:
<nil>
You're using |
which pipes the output of the previous command into the next. Instead you probably need two commands in Go. Use string(out)
as input to the second one rather than trying to pipe it and compose the two bash commands into one Go command.
// this is pseudo code
cmd2 := exec.Command("head", "args1", string(out))
Basically, you have to do the piping yourself and use two separate commands rather than trying to invoke command once with commands that are composed using pipe.