dongtaotao19830418
dongtaotao19830418
2015-09-24 14:59

如何在Go中使用多个iname匹配器执行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条回答

  • dongyilai4214 dongyilai4214 6年前

    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", ")")
    
    点赞 评论 复制链接分享