1

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.

Rolando
  • 58,640
  • 98
  • 266
  • 407
  • Possible duplicate of [Make .gitignore ignore everything except a few files](http://stackoverflow.com/questions/987142/make-gitignore-ignore-everything-except-a-few-files) – Jack Gore Mar 31 '17 at 20:30
  • I want to get a count of lines in a file. .gitignore has nothing to do with this. I need to be able to execute this on command line. – Rolando Mar 31 '17 at 22:14

1 Answers1

2

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.

Matt McHenry
  • 20,009
  • 8
  • 65
  • 64