Rolando
Rolando

Reputation: 62704

How to get a count of the total number of lines in almost all files in git repository?

I have a problem where I have files in a directory with extensions .doc, .js, .pdf, .md, .txt

I want to ignore all files except .txt and get a count of the total number of lines in those files.

Using this command, I can ignore ones with .md, but how do I include other extensions?

git ls-files *[^.md] | xargs cat | wc -l

I tried:

git ls-files *[^.md|^.pdf||^.doc] | xargs cat | wc -l

but that does not work.

Upvotes: 1

Views: 244

Answers (1)

Matt McHenry
Matt McHenry

Reputation: 20929

You don't need git ls-files to do the work of limiting the files for you, you can just insert a grep into your pipeline for that. This variant is affirmative, selecting only the extensions you list:

git ls-files | egrep '\.(doc|js|pdf|md)$' | xargs cat | wc -l

This variant is negative, excluding .txt and keeping anything else:

git ls-files | egrep -v '\.txt$' | xargs cat | wc -l

Note, however, that wc -l doesn't really give any meaningful output for binary files like Word docs and PDFs.

Upvotes: 2

Related Questions