• Contact
  • Contact Us
  • Disclamer
  • Home 1
  • Home 2
  • Home 3
  • Privacy Policy
Sunday, May 25, 2025
How VPS - How to use/setup VPS
  • Login
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
How VPS - How to use/setup VPS
No Result
View All Result
Home Operating System Linux

cloc – Count Lines of Code in Many Programming Languages

How VPS by How VPS
November 2, 2018
in Linux, Operating System
0
0
SHARES
47
VIEWS
Share on FacebookShare on Twitter

Contents

  1. How to Install and Use Cloc in Linux Systems
    1. Conclusion

While working on different projects, sometimes you might be required to provide a report or statistics of your progress, or simply to calculate the value of your code.

There is this simple yet powerful tool called “cloc – count lines of code” that allows you to count all number of your code and exclude comments and blank lines at the same time.

It is available in all major Linux distributions and supports multiple programming languages and file extensions and does not have any specific requirements to be used.

In this tutorial you are going to learn how to install and use cloc on your Linux system.

How to Install and Use Cloc in Linux Systems

Installing cloc is easy and simple. Below you can see how to install cloc in different operating systems with their related package managers:

$ sudo apt install cloc                  # Debian, Ubuntu
$ sudo yum install cloc                  # Red Hat, Fedora
$ sudo dnf install cloc                  # Fedora 22 or later
$ sudo pacman -S cloc                    # Arch
$ sudo emerge -av dev-util/cloc          # Gentoo https://packages.gentoo.org/packages/dev-util/cloc
$ sudo apk add cloc                      # Alpine Linux
$ sudo pkg install cloc                  # FreeBSD
$ sudo port install cloc                 # Mac OS X with MacPorts
$ brew install cloc                      # Mac OS X with Homebrew
$ npm install -g cloc                    # https://www.npmjs.com/package/cloc

Cloc can be used to count lines in particular file or in multiple files within directory. To use cloc simply type cloc followed by the file or directory which you wish to examine.

Here is an example from a file in bash. The file in question contains the following code in bash:

$ cat bash_script.sh
cloc – Count Lines of Code in Many Programming Languages

Linux Bash Script

Now lets run cloc on it.

$ cloc bash_script.sh
cloc – Count Lines of Code in Many Programming Languages

Count Lines in File

As you can see it counted the number of files, blank lines, comments and lines of code.

Another cool feature of cloc is that can even be used on compressed files. For example, I have downloaded the latest WordPress archive and ran cloc on it.

$ cloc latest.tar.gz

Here is the result:

cloc – Count Lines of Code in Many Programming Languages

Count Lines on Compressed File

You can see that it recognizes the different types of code and separates the stats per language.

In case you need to get a report for multiple files in a directory you can use “--by-file” option, that will count the lines in each file and provide a report for them. This may take a while for projects with many files and thousands of lines of code.

The syntax is as follows:

$ cloc --by-file <directory>
cloc &#8211; Count Lines of Code in Many Programming Languages

Count Lines on Multiple Files

While the help of cloc is easily readable and understandable, I will include some of the extra options that can be used with cloc some users may find useful.

  • --diff <set1> <set2> – computes the differences in code between the source files of set1 and set2. The input can be a mix of files and directories.
  • --git – forces the inputs to be recognized as git targets if the same are not first identified as file or directory names.
  • --ignore-whitespace – ignores the horizontal whitespace when comparing files with --diff.
  • --max-file-size=<MB> – if you want to skip files larger than the given amount MB.
  • --exclude-dir=<dir1>,<dir2> – exclude given comma separated directories.
  • --exclude-ext=<ext1>,<ext2> – exclude the given file extensions.
  • --csv – export results to CSV file format.
  • --csv-delimiter=<C> – use the character <C> as the delimiter.
  • --out=<file> – save the results to <file>.
  • --quiet – suppress all information messages and show only the final report.
  • --sql=<file> – write the results as create and insert statements that can be read by a database program such as SQLite.
Conclusion

Cloc is a little useful utility that is definitely good to have in your arsenal. While it may not be used on a daily basis, it can help you when you have to generate some report or if you are just curious how is your project going.

Source: tecmint.com

Tags: Linux Commandslinux guidelinux vps setup guide
Previous Post

How to Use ‘fsck’ to Repair File System Errors in Linux

Next Post

Bluehost Review

Next Post
Bluehost Review

Bluehost Review

Leave a Reply Cancel reply

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

No Result
View All Result

Recent Post

Install Imagemagick on CentOS
CentOS

Install Imagemagick on CentOS

by How VPS
June 28, 2023
0

This is how I installed Imagemagick on a vanilla CentOS server Start off by installing the prerequisites yum install php-pear...

Read more
how to Check phpinfo

How to Check phpinfo of Hosting or VPS?

June 28, 2023
Failed to download metadata for repo 'appstream' on Centos 8

How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

February 25, 2022
How to Fix MySQL Error "Plugin 'InnoDB' registration as a STORAGE ENGINE failed"?

How to Fix MySQL Error “Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed”?

November 17, 2020
How to optimize Mysql or MariaDB

How to optimize Mysql or MariaDB

November 3, 2020

Recent News

  • Install Imagemagick on CentOS
  • How to Check phpinfo of Hosting or VPS?
  • How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

Category

  • Arch
  • Authentication
  • Backups
  • BSD
  • Centmin Mod
  • CentOS
  • Control Panels
  • CoreOS
  • CWP
  • Debian
  • Directadmin
  • Encryption
  • Fedora
  • Firewalls
  • Hocvps Script
  • Hosting providers
  • Kloxo-MR
  • Linux
  • Mitigations
  • Operating System
  • Plesk
  • Reviews
  • Securing VPS/Servers
  • Security Patches
  • SSL Certificates
  • Uncategorized
  • Upgrading
  • VPS/Servers management guides
  • Vulnerability Detection
  • Web servers software
  • Webhosting Control Panel
  • About
  • Advertise
  • Careers
  • Contact

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Thabet