2015-09-24 14:59



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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


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