How to count total number of lines of found files?
Solution 1:
If your version of wc
and find
support the necessary options:
find . -name pattern -print0 | wc -l --files0-from=-
which will give you per-file counts as well as a total. If you want only the total:
find . -name pattern -print0 | wc -l --files0-from=- | tail -n 1
Another option for versions of find
that support it:
find . -name pattern -exec cat {} + | wc -l
Solution 2:
$ find . -name '*.txt' -exec cat '{}' \; | wc -l
Takes each file and cat
s it, then pipes all that through wc
set to line counting mode.
Or, [untested] strange filename safe:
$ find . -name '*.txt' -print0 | xargs -0 cat | wc -l
Solution 3:
Unfortunately the output of :
find . -iname "yourpattern" -exec cat '{}' \; |wc -l
inserts extra lines. In order to get a reliable line count you should do:
find . -name "yourpattern" -print0 | xargs -0 wc -l
This way you handle spaces correctly, get a line count for each file, and the total line count, faster and in style!!!
Solution 4:
Another easy way to find no. lines in a file:
wc -l filename
Example:
wc -l myfile.txt