Published on

Shell: Count total number of lines in the files recursively by file extension

Today I wanted to find out how many lines in total does all of the .md files has, in the codebase I am working on at work (purely out of curiosity).

Wrapping grep with find in a for loop did the trick.

total=0
find . -type f -name "*.md" | while read FILE; do
     count=$(grep -c ^ < "$FILE")
     echo "$FILE has $count lines."
     let total=total+count
done
echo "Total lines: $total"

And wollah! I got Total lines: 6188 :smile: