Mary Knize

Lines of code and word counts with xargs and wc

5 min read


The other day I finished a new project and was interested in how many lines of code I'd written. I wanted find each file recursively within a parent directory, count the lines in each file, then get a total once all files were found and tallied. After a bit of searching, I found that piping a list of files to xargs and using wc to count lines or words was a good way to quickly get an approximate count.

How does xargs work? From the manual page (man xargs):

xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial-arguments followed by items read from standard input. Blank lines on the standard input are ignored.

One possible pitfall of xargs, from the manual, is that blanks or newlines in filenames won't be processed correctly. Luckily, I rarely save filenames with blanks or newlines, but this is good to know.

Because Unix filenames can contain blanks and newlines, this default behaviour is often problematic; filenames containing blanks and/or newlines are incorrectly processed by xargs. In these situations it is better to use the -0 option, which prevents such problems. When using this option you will need to ensure that the program which produces the input for xargs also uses a null character as a separator. If that program is GNU find for example, the -print0 option does this for you.

And how does wc work? Again, from the manual page (man wc):

Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified. A word is a non-zero-length sequence of characters delimited by white space.

The options below may be used to select which counts are printed, always in the following order: newline, word, character, byte, maximum line length.

-c, --bytes print the byte counts

-m, --chars print the character counts

-l, --lines print the newline counts

-w, --words print the word counts

Counting lines

To count lines of code (or, more accurately, newlines in files within a directory) what I have to do is do a find for files that I want to count. In this particular case, I wanted files with both .js and .jsx extensions. Then, I pipe those filenames into xargs and run wc -l to count the lines of each file.

find [directory] -name '*.extension' | xargs wc -l

As an example, I'll list out the line counts for the components, templates, and pages that comprise this blog:

find ~/gatsby-blog/src/ -name '*.js*' | xargs wc -l

   92 /home/maryknize/gatsby-blog/src/pages/photos.js
   16 /home/maryknize/gatsby-blog/src/pages/404.js
  176 /home/maryknize/gatsby-blog/src/pages/art.js
   72 /home/maryknize/gatsby-blog/src/pages/projects.js
  113 /home/maryknize/gatsby-blog/src/pages/blog.js
  286 /home/maryknize/gatsby-blog/src/pages/index.js
   44 /home/maryknize/gatsby-blog/src/pages/microblog.js
   17 /home/maryknize/gatsby-blog/src/pages/about.js
  106 /home/maryknize/gatsby-blog/src/components/header.js
   54 /home/maryknize/gatsby-blog/src/components/layout.js
  101 /home/maryknize/gatsby-blog/src/components/seo.js
   52 /home/maryknize/gatsby-blog/src/components/duolingo.js
   31 /home/maryknize/gatsby-blog/src/components/microblog.js
  104 /home/maryknize/gatsby-blog/src/templates/blog-post.js
   50 /home/maryknize/gatsby-blog/src/templates/image-page.js
   81 /home/maryknize/gatsby-blog/src/templates/photo-page.js
 1395 total

I really like that every file is listed with line counts individually and there's a total once all files have been counted. If one file had a much larger line count than the others, I would consider splitting it into some smaller components. In this case, I see that my index.js file is much longer than the rest of the files listed, and the art page is also larger. I actually know that there's a component that I've created on both pages that I should abstract out to its own file. I also know that index.js has a large about of commented lines from things I've tried in the past. Once I finish those two tasks, both files should be much smaller.

Counting words

For fun, I thought I'd also count the words I've written so far on this blog. To do that, I replace the -l flag with -w. find [directory] -name "*.extension" | xargs wc -w

find .. -name '*.md' | xargs wc -w

  1208 ../books/
   836 ../books/
   178 ../programming/
  1171 ../programming/
   808 ../programming/
   220 ../programming/
    78 ../programming/
   510 ../programming/
   307 ../programming/
   965 ../programming/
   316 ../programming/
  1655 ../programming/
   416 ../programming/
  1503 ../programming/
  1266 ../programming/
   712 ../programming/
   249 ../programming/
   396 ../programming/
  2703 ../programming/
   928 ../programming/
  1162 ../recipes/
  1444 ../recipes/
   722 ../recipes/
   892 ../recipes/
   449 ../recipes/
   482 ../recipes/
  1106 ../home_automation/
  1297 ../home_automation/
   316 ../life/
  1230 ../life/
   434 ../life/
   661 ../life/
   490 ../life/
   341 ../art/
   481 ../art/
   637 ../art/
  1304 ../advent_of_code/
   557 ../advent_of_code/
   913 ../advent_of_code/
   894 ../advent_of_code/
   876 ../advent_of_code/
  1508 ../advent_of_code/
  1080 ../advent_of_code/
   947 ../advent_of_code/
   838 ../advent_of_code/
   495 ../web_accessibility/
   728 ../web_accessibility/
   459 ../writing/
   733 ../writing/
 39901 total
Canonical URL