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.

Published by jukkasoft

Me: a code enthusiasta, technology optimist, but also a realist. In my blog something like 80% of the articles are technology related - mostly in English and Finnish. I like to envision and ponder about things, how life could be, what we are doing wrong today, and so on. "Invented" a facebook -like system in 2000, as a short product pamphlet. Haven't yet realized the 20+ ideas that might have become big things. Maybe learning this skill one of these days!

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 )

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s

%d bloggers like this: