【问题标题】:Use wc on all subdirectories to count the sum of lines在所有子目录上使用 wc 来计算行数
【发布时间】:2012-11-23 13:19:45
【问题描述】:

如何用wc计算所有子目录中所有文件的所有行数?

cd mydir
wc -l *
..
11723 total

man wc 建议wc -l --files0-from=-,但我不知道如何将所有文件的列表生成为NUL-terminated names

find . -print | wc -l --files0-from=-

没用。

【问题讨论】:

  • find . -name '*'|xargs wc -l 可能会有所帮助。

标签: linux bash text-files console-application


【解决方案1】:

你可能想要这个:

find . -type f -print0 | wc -l --files0-from=-

如果你只想要总行数,你可以使用

find . -type f -exec cat {} + | wc -l

【讨论】:

  • 我使用了find . -type f -print0 | wc -l --files0-from=-,它工作正常并在最后返回总和。
  • 在 mac 上,第一次给出:wc: illegal option -- -
【解决方案2】:

也许您正在寻找findexec 选项。

find . -type f -exec wc -l {} \; | awk '{total += $1} END {print total}'

【讨论】:

  • 这是低效的,因为您要为每个文件生成一个新的wc:-(
  • 如果你碰巧有这样的文件,这完全被破坏了:试试touch $'hello\n1000000'。惊喜!
【解决方案3】:

要计算您可以使用的特定文件扩展名的所有行,

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

如果你想在两种或更多不同类型的文件上使用它,你可以使用 -o 选项

find . -name '*.fileextension1' -o -name '*.fileextension2' | xargs wc -l

【讨论】:

  • 这会中断任何名称中带有空格或 `` 的文件。请参阅accepted answer 了解如何正确使用此方法。
  • 喜欢这个解决方案,即使它在所有情况下显然都不够用。像魅力一样为我的目的工作。
【解决方案4】:

另一种选择是使用递归 grep:

grep -hRc '' . | awk '{k+=$1}END{print k}'

awk 只是简单地将数字相加。使用的grep 选项是:

   -c, --count
          Suppress normal output; instead print a count of matching  lines
          for  each  input  file.  With the -v, --invert-match option (see
          below), count non-matching lines.  (-c is specified by POSIX.)
   -h, --no-filename
          Suppress the prefixing of file names on  output.   This  is  the
          default  when there is only one file (or only standard input) to
          search.
   -R, --dereference-recursive
          Read all files under each directory,  recursively.   Follow  all
          symbolic links, unlike -r.

因此,grep 会计算匹配任何内容 ('') 的行数,因此本质上只是计算行数。

【讨论】:

    【解决方案5】:

    我会建议类似的东西

    find ./ -type f | xargs wc -l | cut -c 1-8 | awk '{total += $1} END {print total}'
    

    【讨论】:

    • 所有不是目录的东西;在大多数情况下,将与 -type f 相同
    • 除了块设备节点、字符设备节点、命名管道、套接字……至少其中一个会破坏整个练习……最好坚持-type f……
    【解决方案6】:

    根据ДМИТРИЙ МАЛИКОВ的回答:

    使用格式化计算 java 代码行数的示例:

    一个班轮

    find . -name *.java -exec wc -l {} \; | awk '{printf ("%3d: %6d %s\n",NR,$1,$2); total += $1} END {printf ("     %6d\n",total)}'
    

    awk 部分:

    { 
      printf ("%3d: %6d %s\n",NR,$1,$2); 
      total += $1
    } 
    END {
      printf ("     %6d\n",total)
    }
    

    示例结果

      1:    120 ./opencv/NativeLibrary.java
      2:     65 ./opencv/OsCheck.java
      3:      5 ./opencv/package-info.java
            190
    

    【讨论】:

      【解决方案7】:

      在这里玩游戏有点晚了,但这不也行吗? find . -type f | wc -l

      这会计算“find”命令输出的所有行。您可以微调“查找”以显示您想要的任何内容。我用它来计算子目录的数量,在一个特定的子目录中,在深树中:find ./*/*/*/*/*/*/TOC -type d | wc -l。输出:76435。 (只是在没有所有中间星号的情况下进行查找会产生错误。)

      【讨论】:

      • 不,我认为这会返回类似于文件数的内容。不是所有文件中所有行的总和。
      • 啊,现在看看。是的,不会的。 :)
      猜你喜欢
      • 2010-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-27
      • 2013-01-31
      • 1970-01-01
      • 2020-07-30
      相关资源
      最近更新 更多