DEV Community

Cover image for How to count lines of code you written.
Navraj Sandhu
Navraj Sandhu

Posted on

How to count lines of code you written.

In this short tutorial we are gonna see how to count lines of code that you've written.

Before we get started you should have node, npm, npx installed in your machine.

Let's start

  • Open terminal and cd to your project directory.
  • Run the following command
npx sloc .
Enter fullscreen mode Exit fullscreen mode

or you can install sloc package globaly.

npm install -g sloc
Enter fullscreen mode Exit fullscreen mode

run command with sudo if you using linux or mac.

then you can run

sloc .
Enter fullscreen mode Exit fullscreen mode

If you want to exclude file or directory( Like venv or node modules) you can do like that.

sloc . -e <file>|<directory>
Enter fullscreen mode Exit fullscreen mode

Example :

  • Here I have a django project in which i have venv folder that i wanna exclude.

Example-terminal

Thanks for reading.

Happy coding 😎.

Top comments (1)

Collapse
 
moopet profile image
Ben Sinclair

You shouldn't need to run anything with sudo for this kind of purpose.

I'd like to point out that "source lines of code" is a very dodgy metric to use for anything. It depends entirely on things like the language you use and the formatting you use and the sloc algorithm your software is using to measure things. Not only that, but the number of lines you've written rarely has anything to do with how much work you've done, how much progress a project has made or how many features have been completed.

It's probably useful if you want a summary of how much has changed since a previous date, but you can get that using a diff in your VCS. For instance, git diff --name-only old-version-tag is likely to give you something that's of more practical use than knowing your code includes 143 block comments.