英文:
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", ")")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论