如何在Go中使用多个iname匹配器执行find命令?

时间:2015-09-24 14:59:45

标签: shell go command

我需要通过Go编程语言执行这种命令:

find /some/dir/path -type f \( -iname \*.zip -o -iname \*.tar -o -iname \*.rar \)

我发现了exec.Command并尝试了各种执行find命令的方法, e.g。

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命令?

1 个答案:

答案 0 :(得分:1)

exec.Command不是shell,因此您无需在命令中转义特殊字符。通过命令处理每个参数的确切方式。

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