Use wc on all subdirectories to count the sum of lines

自古美人都是妖i 提交于 2019-11-30 02:43:07

You probably want this:

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

If you only want the total number of lines, you could use

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

Perhaps you are looking for exec option of find.

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

To count all lines for specific file extension u can use ,

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

if you want it on two or more different types of files u can put -o option

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

Another option would be to use a recursive grep:

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

The awk simply adds the numbers. The grep options used are:

   -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.

The grep, therefore, counts the number of lines matching anything (''), so essentially just counts the lines.

I would suggest something like

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

Bit late to the game here, but wouldn't this also work? find . -type f | wc -l

This counts all lines output by the 'find' command. You can fine-tune the 'find' to show whatever you want. I am using it to count the number of subdirectories, in one specific subdir, in deep tree: find ./*/*/*/*/*/*/TOC -type d | wc -l . Output: 76435. (Just doing a find without all the intervening asterisks yielded an error.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!