1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Get Word Count For All Files In Git Repo as a Unix TIL

This commit is contained in:
jbranchaud
2025-02-08 18:07:54 -06:00
parent 77cc07a908
commit 409201611f
2 changed files with 34 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1586 TILs and counting..._
_1587 TILs and counting..._
See some of the other learning resources I work on:
- [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators)
@@ -1536,6 +1536,7 @@ See some of the other learning resources I work on:
- [Get Matching Filenames As Output From Grep](unix/get-matching-filenames-as-output-from-grep.md)
- [Get The SHA256 Hash For A File](unix/get-the-sha256-hash-for-a-file.md)
- [Get The Unix Timestamp](unix/get-the-unix-timestamp.md)
- [Get Word Count For All Files In Git Repo](unix/get-word-count-for-all-files-in-git-repo.md)
- [Global Substitution On The Previous Command](unix/global-substitution-on-the-previous-command.md)
- [Globbing For All Directories In Zsh](unix/globbing-for-all-directories-in-zsh.md)
- [Globbing For Filenames In Zsh](unix/globbing-for-filenames-in-zsh.md)

View File

@@ -0,0 +1,32 @@
# Get Word Count For All Files In Git Repo
As part of gathering numbers for [A Decade of TILs](), I wanted to get an word
count of all the TIL markdown files I've committed to this project over its 10
year history. By using `git ls-files` with a pattern, I can get a list of all
file names. Then with `xargs` I can pass that entire list to `wc -w` which
gives a word count of each. The final line that `wc -w` outputs is a sum total
of all the file word counts. Lastly, piping that through `tail -n1` gives me
just that last total count line.
```bash
$ git ls-files "*/**.md" | xargs wc -w | tail -n1
206816 total
```
Since the `tail -n1` obfuscates what the `wc -w` is doing, here is what that
looks like before that final pipe.
```bash
$ git ls-files "*/**.md" | tail -n3 | xargs wc -w
115 zsh/add-to-the-path-via-path-array.md
190 zsh/link-a-scalar-to-an-array.md
214 zsh/use-a-space-to-exclude-command-from-history.md
519 total
```
I can even clean up the final output a bit more with `awk`:
```bash
$ git ls-files "*/**.md" | xargs wc -w | tail -n1 | awk '{print $1}'
206816
```