Ohcount – The Source Code Line Counter And Analyzer
Ohcount is simple command line utility that analyzes the source code and prints the total number of lines of a source code file. It is not just source code line counter, but also detects the popular open source licenses, such as GPL, within a large directory of source code. Additionally, Ohcount can also detect code that targets a particular programming API such as KDE or Win32. As of writing this guide, Ohcount currently supports over 70 popular programming languages. It is written in C programming language and is originally developed by Ohloh for generating the reports at www.openhub.net.
In this brief tutorial, we are going to how to install and use Ohcount to analyze source code files in Debian, Ubuntu and its variants like Linux Mint.
Ohcount – The source code line counter
Ohcount is available in the default repositories in Debian and Ubuntu and its derivatives, so you can install it using APT package manager as shown below.
$ sudo apt-get install ohcount
Ohcount usage is quite simple.
All you have to do is go to the directory where you have the source code that you want to analyze and ohcount program.
Say for example, I am going to analyze the source of code of coursera-dl program.
$ cd coursera-dl-master/
Here is the line count summary of Coursera-dl program:
As you can see, the source code of Coursera-dl program contains 141 files in total. The first column specifies the name of programming languages that the source code consists of. The second column displays the number of files in each programming languages. The third column displays the total number of lines in each programming language. The fourth and fifth columns displays how many lines of comments and its percentage in the code. The sixth column displays the number of blank lines. And the final and seventh column displays total line of codes in each language and the gross total of coursera-dl program.
You can also mention the complete path of the directly like below.
$ ohcount coursera-dl-master/
The path can be any number of individual files or directories. Directories will be probed recursively. If no path is given, the current directory will be used.
If you don’t want to mention the whole directory path each time, just CD into it and use ohcount utility to analyze the codes in that directory.
To count lines of code per file, use -i flag.
$ ohcount -i
Ohcount utility can also show the annotated source code when you use -a flag.
$ ohcount -a
As you can see, the contents of all source code files found in this directory is displayed. Each line is prefixed with a tab-delimited language name and semantic categorization (code, comment, or blank).
Some times, you just want to know the license used in the source code. To do so, use -l flag.
$ ohcount -l lgpl3, coursera_dl.py gpl coursera_dl.py
Another available option is -re, which is used to print raw entity information to the screen (mainly for debugging).
$ ohcount -re
To find all source code files within the given paths recursively, use -d flag.
$ ohcount -d
The above command will display all source code files in the current working directory and the each file name will be prefixed with a tab-delimited language name.
To know more details and supported options, run:
$ ohcount --help
Ohcount is quite useful for developers who wants to analysis the code written by themselves or other developers, and check how many lines that code contains, which languages have been used to write those codes and the license details of the code etc.