【问题标题】:How to count all the lines of code in a directory recursively using or operator to include several file types如何使用或运算符递归计算目录中的所有代码行以包含多种文件类型
【发布时间】:2016-11-20 11:44:36
【问题描述】:

我正在阅读这个答案:How to count all the lines of code in a directory recursively?

但它只适用于 php 文件。我想递归地计算包括javascript、html、css和php文件在内的代码组合行数。

我试过这个find . -name '*.php' |'*.js' | xargs wc -l

但它不起作用。

注意:我使用的是 OS X El Capitan。

【问题讨论】:

    标签: bash shell


    【解决方案1】:

    避免使用xargs

    find \( -name '*.php' -o -name '*.js' -o -name '*.html' \) -exec wc -l {} +
    
    • \( \) 分组以便所有结果都给wc 命令
    • \( \) 中添加所需数量的-o -name
    • 使用iname 而不是name 来忽略文件名的大小写
    • 默认情况下,find 适用于当前目录

    参考:

    【讨论】:

    • 您可能希望在 * 和每个实例的文件扩展名之间添加一个点。
    【解决方案2】:

    试一试:

    find . -name '*.php' | xargs wc -l

    包含文件:

    find . -name '*.php' -o -name '*.js' | xargs wc -l

    不包括某些路径:

    find . -name "*.php" -not -path "./tests*" | xargs wc -l

    【讨论】:

    • 谢谢。如果我想包含 html 和 css 文件怎么办? -o 是 or 运算符吗?
    • @RobertRocha 是的,只要包含-o -name,只要您需要搜索更多文件。
    【解决方案3】:

    这是一个单行。它使用findregex 选项来查找匹配的扩展名,并使用wc --files0-from=- 参数从标准输入中获取文件列表

    find -regex '.*\(php\|js\|css\|html?\)' -printf '%p\0' |wc -l --files0-from=-
    

    【讨论】:

      猜你喜欢
      • 2010-11-24
      • 2020-06-08
      • 2020-07-30
      • 2012-02-27
      • 1970-01-01
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      • 2019-11-10
      相关资源
      最近更新 更多