简体   繁体   English

如何计算所有文件行

[英]How to count all the lines of files

I also need the directory name to be outputs as well. 我还需要目录名称作为输出。 What I was able to do is to output the total number of lines in all directories with directory name. 我能做的是用目录名输出所有目录中的总行数。

find . -name '*.c' | xargs wc -l | xargs -I{} dirname {} | xargs -I{} dirname {}

Here is a script: 这是一个脚本:

#!/usr/bin/env bash

for dir in */; do (
    cd "$dir"
    count=$(find . -name '*.c' -print0 | xargs -0 grep '[;]$' | wc -l)
    echo -e "${count}\t${dir}"
) done

If you want numbers for each sub-directory: 如果您想要每个子目录的数字:

#!/usr/bin/env bash

for dir in $(find . -type d); do (
    cd "$dir"
    count=$(find . -maxdepth 1 -name '*.c' -print0 | \
        xargs -0 grep '[;]$' | wc -l)
    echo -e "${count}\t${dir}"
) done

Using -maxdepth 1 makes sure the calculation is only done in the current directory, not its sub-directories. 使用-maxdepth 1可确保计算仅在当前目录中完成,而不是在其子目录中完成。 So each file is counted once. 所以每个文件都计算一次。

I have jumbled up a mixture of bash commands mostly GNU -specific, make sure you have them, GNU grep and GNU Awk 我混淆了混合的bash命令,主要是GNU特定的,确保你有它们, GNU grepGNU Awk

find . -type f -print0 | xargs -0 grep -c ';$' | \
  awk -F":" '$NF>0{cmd="dirname "$1; while ( ( cmd | getline result ) > 0 ) {printf "%s\t%s\n",result,$2} close(cmd) }'

The idea is grep -c returns the pattern count in format, file-name:count , which I am passing it to GNU Awk to filter those files whose count is greater than zero and print the directory of the file containing it and the count itself. 想法是grep -c以格式返回模式计数, file-name:count ,我将它传递给GNU Awk来过滤那些计数大于零的文件并打印包含它的文件的目录和计数本身。

As a fancy one-liner as they call it these days, 这些天他们称之为花哨的单行,

find . -type f -print0 | xargs -0 grep -c ';$' | awk -F":" '$NF>0{cmd="dirname "$1; while ( ( cmd | getline result ) > 0 ) {printf "%s\t%s\n",result,$2} close(cmd) }'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM