grep
can do the counting for you with its -c
flag, so wc -l
is not needed. Also, grep
takes multiple files as input, if you provide them.
For example,
grep -c '^>' some/dir/*.fa
To do it recursively, use grep -Rc '^>' dirname
if you have a grep
that knows the -R
option (this would run over all files), otherwise use find
:
find dirname -type f \( -name '*.fa' -o -name '*.fasta' \) -exec grep -c '^>' /dev/null {} +
The extra /dev/null
in the command above ensures that grep
gets at least two input files, which in turn ensures that it will always display the name of the file that it processes (it does not do that with a single input file). One could also use -H
with grep
, although this is a non-standard option.
Or, with your original command plugged into a loop that is fed with pathnames from find
:
find dirname -type f \( -name '*.fa' -o -name '*.fasta' \) -exec sh -c '
for pathname do
printf "Counting in %s...\n" "$pathname"
grep "^>" "$pathname" | wc -l
done' sh {} +
Since your command does not report the filename by itself, I added a printf
statement that mentions it.
Related:
for
loop.