我需要通过Go编程语言执行这种命令:
find /some/dir/path -type f \( -iname \*.zip -o -iname \*.tar -o -iname \*.rar \)
我发现了exec.Command并尝试了各种执行find命令的方法,
例如

exec.Command("find", dir, "-type", "f", "\\( -iname \\*.zip -o -iname \\*.tar -o -iname \\*.rar \\)")
exec.Command("find", dir, "-type", "f", "-iname", "*.zip", "-o", "-iname", "*.tar", "-o", "-iname", "*.rar")
exec.Command("find", dir, "-type", "f", "\\(", "-iname", "\\*.zip", "-o", "-iname", "\\*.tar", "-o", "-iname", "\\*.rar", "\\)")

以上都不对我有用。有没有办法做到这一点,还是我只需要在Go中发出3个单独的find命令?

最佳答案

exec.Command不是外壳程序,因此您无需在命令中转义特殊字符。按照您希望命令如何处理的方式传递每个参数。

exec.Command("find", dir, "-type", "f", "(", "-iname", "*.zip", "-o", "-iname", "*.tar", "-o", "-iname", "*.rar", ")")

10-06 12:01