Using jq
only:
jq -s 'map(length) | add' ./*.json
-s
/--slurp
makes jq
read its input as a single array, running the specified filter only once against it. map
is used to run length
for each element of that virtual array, returning an array of numbers, and add
finally sums them.
To also make sure not to hit the command line length limit (but note that this would also recursively process files in subdirectories1):
find . -name "*.json" -exec jq 'length' {} + | jq -s 'add'
Found files are passed to jq 'length'
in batches whose size depends on the maximum command line length allowed on your system. Since find
may run jq
more than once, making it slurp its input won't reliably work and its output is piped to a second (slurping) jq
instead.
1 Several Q/As on this site show how to prevent find
from descending into directories; for instance, Using "find" non-recursively?