wc
takes the list of files whose bytes/chars/w
ords/lines to c
ount as arguments.
When called with no argument, it reports those bytes/chars/words/lines in its stdin. So if you're piping find
to wc -l
, you'll get the number of newline characters in the output of find
, so that'll be the number of found files plus the number of newline characters in their paths.
The GNU implementation of wc
can also take the list of files NUL-delimited from a file with the --files0-from
option, where it treats -
as meaning stdin (not the file called -
), so you can do:
find . -name package.json -type f -print0 |
wc -l --files0-from=-
With any standard find
or wc
implementation, you could get find
to pass the list of file paths as arguments to wc
with:
find . -name package.json -type f -exec wc -l {} +
But if there's a large number of matching files, that could end up running wc
several times resulting in several occurrences of a total
line.
wc
prints the total
line when given at least 2 files to process, so to skip the total
line, you could do:
find . -name package.json -type f -exec wc -l {} ';'
Though that would be very inefficient as forking a process and executing a command for each file is quite expensive.
If it's the total you're actually interested in, then you'd do:
find . -name package.json -type f -exec cat {} + | wc -l
Where we feed the concat
enation of the contents of those files to wc
.
With zsh
and any wc
, you could do:
wc -l -- **/package.json(D.)
(D
for D
otglob to get hidden ones as well like find
does and .
to only include regular files as the equivalent of -type f
).
That has the advantage of giving you a sorted list and avoid the ./
prefix.
This time, if there are no or too many matching files, you'll get an error.
With GNU du
, you can avoid those by passing the glob expansion NUL-delimited to wc -l --files0-from=-
with:
print -rNC1 -- **/package.json(ND.) | wc -l --files0-from=-
Also beware that in the json format, newline characters (which wc -l
counts) are not significant so I'm not sure that's a useful metric you're getting.
You could return the number of elements in some array in those files for instance instead with:
find . -name package.json -type f -exec \
jq -r '[.devDependencies|length,input_filename]|@csv' {} +
(assuming the file paths are UTF-8 encoded text and here giving you the result in CSV format).