Counting Lines in Text Files

04 Sep 2014

Let’s assume we have a directory of text files, and we need the grand total line count across all of the files.

My first attempt was this:

Bash
1
find . -type f -print0 | xargs -0 wc -l

I expected this to not give me what I wanted.  I expected it to show me line totals by file - but I was surprised to see a grand total line count at the end.

Then, I saw a variation on the above approach, in a Stack Overflow answer:

How to count all the lines of code in a directory recursively?

Bash
1
(find . -type f -print0 | xargs -0 cat ) | wc -l

Simple and effective - just the grand total.