在Go中如何执行带有多个iname匹配器的find命令?

huangapple go评论90阅读模式
英文:

How do I execute a find command with multiple iname matchers in Go?

问题

我需要通过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命令?

英文:

I need to execute this sort of command through the Go programming language:

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

I discovered exec.Command and tried various ways of executing the find command,
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", "\\)")

None of the above have worked for me. Is there a way to do this, or am I just going to have to issue 3 separate find commands in Go?

答案1

得分: 1

exec.Command不是一个shell,所以你不需要在命令中转义特殊字符。按照你希望命令处理的方式,将每个参数传递给它。

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

exec.Command isn't a shell, so you don't need to escape special characters in your command. Pass each argument in exactly how you want it processed by the command.

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

huangapple
  • 本文由 发表于 2015年9月24日 22:59:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/32764670.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定