How to count lines of source code in Linux

For various reasons, you may want to know how many lines of source code a given software program is built in. For example, you want to estimate the effort devoted to developing a program, or to gauge the size and complexity of a program before trying it. There is some controversy as to using source lines of code (SLOC) as a metric to determine the size of a software program, since existing programming languages differ greatly in terms of clarify and brevity.

If you would like to count the number of source code lines quickly, you can do one of the following.

To search all python source files recursively in the current directory, and count the lines of them:

$ find . -name "*.py" | xargs wc -l

To be more precise (e.g., discounting comment lines and blank lines), you can use cloc (short for "Count Lines Of Code") in Ubuntu or Debian, which is a Perl application that counts lines of codes.

$ sudo apt-get install cloc
$ find . -name "*.py" | xargs cloc
       8 text files.
       8 unique files.
       2 files ignored. v 1.53  T=0.5 s (16.0 files/s, 3278.0 lines/s)
Language                     files          blank        comment           code
Python                           8            224            234           1181
SUM:                             8            224            234           1181

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

Leave a comment

Your email address will not be published. Required fields are marked *