在Mac OS X上使用命令find扩展POSIX实用程序?

时间:2014-08-02 10:23:45

标签: regex macos bash terminal find

我尝试在Mac OS X 10.9终端上执行此命令:

find / -type f -regextype posix-extended -regex "(.*A.*a.*.*)|(.*a.*A.*.*)" -exec tail -n 3 '{}' \;

但Bash v 3.2的回应是:

find: -regextype: unknown primary or operator

我在终端联机帮助页面上进行了深入搜索:

man find

man regex

等...

但我一无所获。是否应该更新Mac OS X Bash v3.2?似乎选项" -regextype"未包含在" find"。

我该如何解决?

P.S。 该命令在Ubuntu Linux 14上运行完美。

2 个答案:

答案 0 :(得分:11)

您问题的最简单答案是,在OS X上,您需要使用-E选项而不是-regextype posix-extended

即:

  

find / -type f -regextype posix-extended -regex“(。* A。 a。。*)|(。* a。 A 。。*)“ - exec tail -n 3'{}'\;

变为:

  

找到 -E / -type f -regex“(。* A。 a。。*)|(。* a。 A。。*)“ - exec tail -n 3'{}'\;

除了抱怨的-exec tail命令之外,上面的内容适用于我的10.10安装。

这实际上由手册页的第一个条目覆盖:

 The options are as follows:

 -E      Interpret regular expressions followed by -regex and -iregex primaries as extended (modern) regular expressions rather than basic
         regular expressions (BRE's).  The re_format(7) manual page fully describes both formats.

答案 1 :(得分:0)

您可以使用基本正则表达式:

find / -type f -regex "\(.*A.*a.*.*\)\|\(.*a.*A.*.*\)" -exec tail -n 3 '{}' \;