代码之家  ›  专栏  ›  技术社区  ›  static_rtti

如何从UNIX shell中搜索文件名和文件内容的约束条件?

  •  0
  • static_rtti  · 技术社区  · 16 年前

    是否可以执行全文搜索?如何在文件名和文件内容都有一些约束的情况下进行搜索?

    4 回复  |  直到 16 年前
        1
  •  2
  •   Luc M    16 年前
    find -name whatever -exec grep --with-filename you_search_for_it {} \;
    

    {} 包含返回的文件名 find

    \; 终止find命令

        2
  •  6
  •   smcameron    16 年前

    找到。-名字随便-打印xargs grep随便什么

    将“-l”选项添加到grep以获取文件名。

        3
  •  3
  •   Matthew Slattery    16 年前

    我强烈建议你抓紧 ack 把它用于任何你想做的芬迪-格丽派类型的东西-我每天都用它,无法想象没有它我是如何生活的!在这种情况下,听起来像 ack -G <file-regex> <text-regex> 做你想做的。

        4
  •  2
  •   Dennis Williamson    16 年前

    在某些情况下,globbing会对文件名提供足够的约束:

    shopt -s nullglob    # Bash: prevents "No such file or directory" errors
    grep string {.,[jm]*,{one,two}}/{[a-c],[hlz]}?{earth,mars,venus}[[:ascii:]]*atm*.dat
    

    搜索文件,如:

    ./bZmars_321atmBB111.dat
    m42a/z3venus-a18atm9.dat
    two/aaearth+GHIatm9876.dat