2 Useful Tools to Count Lines of Source Code in Programming Languages

In the realm of software development, managing and understanding codebase metrics is crucial for maintaining code quality, optimizing performance, and ensuring efficient collaboration among development teams.

Tools that assist in analyzing and quantifying code statistics play a pivotal role in this process. Two prominent tools that have gained traction for their simplicity and effectiveness are “SLOC, CLOC, and Code” or simply “scc” and “CLOC” (Count Lines of Code).

What is scc?

SLOC, CLOC, and Code” (scc) is an open-source command-line tool designed to provide developers with quick and insightful code analysis metrics.

It is particularly renowned for its ability to efficiently count lines of code (LOC) in various programming languages such as Python, Java, and C++ to more, offer comprehensive statistics, and assess code complexity.

With a focus on minimalism and efficiency, scc has become a popular choice among developers seeking an unobtrusive yet powerful tool for codebase analysis.

What is Cloc?

CLOC, short for “Count Lines of Code“, is another popular command-line tool used to quantify codebase metrics. Like scc, CLOC helps developers understand the composition of their codebase by counting lines of code, comments, and blank lines.

While not as feature-rich as scc, CLOC excels in its simplicity and ease of use, making it a favored choice for quickly obtaining high-level code statistics.

Install Sloc Cloc and Code (scc) in Linux

To install scc on Linux, you can use the Snap package manager, but first, make sure to ensure that your Linux distribution comes with Snap pre-installed, but if it’s not available, you might need to install it separately.

$ sudo apt install snapd     [On Debian, Ubuntu and Mint]
$ sudo zypper install snapd  [On OpenSUSE]    
----------- On RHEL/CentOS/Fedora and Rocky/AlmaLinux ----------- 
$ sudo dnf install snapd
$ sudo ln -s /var/lib/snapd/snap /snap
----------- On Arch Linux ----------- 
$ git clone https://aur.archlinux.org/snapd.git
$ cd snapd
$ makepkg -si
$ sudo systemctl enable --now snapd.socket
$ sudo ln -s /var/lib/snapd/snap /snap
----------- On Manjaro Linux ----------- 
$ sudo pacman -S snapd
$ sudo systemctl enable --now snapd.socket
$ sudo ln -s /var/lib/snapd/snap /snap

Once snapd is installed, you can run the following commands to install scc and verify the installed version as shown.

$ sudo snap install scc
$ scc --version
Install scc in Linux
Install scc on Linux

You should see the version number of SCC printed on the console.

Install Cloc on Linux

To install “cloc” (Count Lines of Code) on a Linux system, you can use package managers as shown.

$ sudo apt install cloc         [On Debian, Ubuntu and Mint]
$ sudo yum install cloc         [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
$ sudo emerge -a sys-apps/cloc  [On Gentoo Linux]
$ sudo apk add cloc            [On Alpine Linux]
$ sudo pacman -S cloc           [On Arch Linux]
$ sudo zypper install cloc      [On OpenSUSE]    
Install cloc in Linux
Install cloc in Linux

After installing, you can verify that cloc is installed by running the following command in the terminal.

$ cloc --version

Count Lines of Code in File

To count the lines of code in the file hello.py, you would run the following command:

$ scc hello.py
OR
$ cloc hello.py

It will output the number of lines of code, blank lines, comment lines, and physical lines of source code in the file.

Count Lines of Source Code in File
Count Lines of Source Code in File

To count the lines of code in the current directory and its subdirectories, use:

$ scc code/
OR
$ cloc code/
Count Lines of Source Code in Directory
Count Lines of Source Code in the Directory

Here are some additional options that you can use with SCC or Cloc:

  • -l – List all programming languages found in the source code.
  • -c – Only count C code.
  • -s – Only count C++ code.
  • -j – Use multiple threads to speed up the counting process.
  • -o – Output the results in a specific format, such as XML or JSON.

For more advanced usage, use the following command to see available options and examples.

$ scc --help
OR
$ cloc --help

Other Code Counters

If scc and cloc do not suit your needs here are other freely available code counters to consider:

Conclusion

In conclusion, both scc and cloc are valuable tools for code analysis, each with its strengths. scc shines with its comprehensive features and flexibility, while CLOC excels in its simplicity and speed.

Consider the nature of your project, the depth of analysis required, and your familiarity with command-line tools when deciding between these two capable options.

Regardless of your choice, both tools contribute to better code management and enhanced collaboration within development teams.

Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.