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:
32
unix/get-word-count-for-all-files-in-git-repo.md
Normal file
32
unix/get-word-count-for-all-files-in-git-repo.md
Normal 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
|
||||
```
|
||||
Reference in New Issue
Block a user