Lines of code and word counts with xargs and wc
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 (
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 (
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
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.
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