Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

The "wc" Linux Utility For Measuring Lines of Code For A Project

4/3/2018

0 Comments

 
I've recently been building web applications with front-end frameworks like React, Reagent, and Angular 2. I was recently working on an Angualr 2 project and thought, "man, this sure seems like a ton of lines of code", but had no concrete evidence to prove it. After a quick google search I came to this stack overflow question, and the awesome answer(s) therein. 

Some Simple wc Usage

The commands in this post should be entered into any  linux command shell such as the terminal app on a mac. Open it up and run this line of code to create an empty file called "derp.txt". 
touch derp.txt
Then you can pass it to wc like so.
wc derp.txt
This will give you three numbers. If you just created the file with touch they should all be zeros.
Picture
The numbers are not labeled because you should be using wc so much that you have the numbers memorized and will never forget them (or just so that it's easier to pipe the output of this into other things). Just in case you've forgotten, the numbers represent newlines, words, and bytes. Interestingly, you can pass the -c flag for bytes and the -m flag for characters, but they always seem to give the same output on my machine. 

Counting Multiples Files At Once

In its simplest form, let's just say you want to get the total number of lines for all files in your current directory with the ".js" extension. This is simple enough, we can just use the * wildcard. This will print out the value for each of your files and a little total at the bottom.
wc *.txt
Picture

Searching Recursively For Files

The simple wildcard example above won't search recursively through subfolders. Indeed, this is the main question the op is asking in stack overflow example I mentioned. The top voted answer is a nice one and allows you to get the wc metrics recursively for some file type.
find . -name '*.txt' | xargs wc -l

Multiple File Types

If I wanted to really compare Angular, React, and Reagent projects I had to be fair about it. React has only .js files for the core logic and DOM code, but Angular has .ts and .html files for the core logic and DOM code. I found this hidden gem in one of the comments of the second answer on that SO question.
( find . \( -name '*.html' -o -name '*.ts' \) -print0 | xargs -0 cat ) | wc -l

Have Fun With It

I was only looking for a character count tool and found something that can give me characters, line numbers, and words! I think these three along with maybe number of files I think should give you a good measure of actually how large you project is to other similar projects. It's also interesting to compare non-similar projects to see if one may get the job done but with much less code as this may be easier to write, manage, and maintain in the long run. Anyway, that is an argument for another day. Today is the day of wc, and with it you luck may double, you see.
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Want FREE access to
    my daily stock tips 
    ​newsletters??
    Sign up here:

    - Triple Gainers
    - Rippers N' Dippers
    - Growingest Growers
    ​

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2021