Count lines of software source code

How big is this project?

After cloning a Github repo, you might want to check specific source file lengths, in terms of lines of code.

Here’s a simple oneliner:

print lines of code in a python project

find . -type f | grep '\.py$' | xargs wc -l '{}'

what happens in that oneliner?

The whole oneliner thus reads:

“search files, ending in a .py, and then for these files, count the number of lines by using wc command”

“find” is a search tool, that looks for files and folders on a *NIX system. It has huge number of parameters, but we use only the “-type” switch to limit the search results for only “f”, that is, files.

Then piping over with “|” character, the results of find are fed into grep. Grep is a versatile string search (pattern matcher). We are looking now (out of all find results) only those lines that have “.py” in the end (the end of line is a special character in regular expressions, marked with a $ dollar sign).

xargs passes lines to be used as parameter values for command following the xargs itself. So with the last part, we want to run wc (word count tool, but with small letter L “-l” is turns into line count tool) with each line of the previous search result being a parameter to wc.

Oh, and by the way: the oneliner runs on sh / bash shell on Linux.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: