It can be difficult to count the number of lines of code that comprises a certain program, since simply viewing the source code will include comments, whitespace, etc. On Linux systems, the cloc command can be used to count lines of code in one or multiple files, and even sort results by programming language.
The cloc program is especially helpful if you need to measure and submit your progress of a coding project, view coding statistics, or calculate the total value of your code.
In this tutorial, you’ll see how to install the cloc software package on all major Linux distributions, and then use the cloc
command to count the lines of code of various program files.
In this tutorial you will learn:
- How to install cloc on major Linux distros
- How to use the
cloc
command to count lines of code on Linux

Category | Requirements, Conventions or Software Version Used |
---|---|
System | Any Linux distro |
Software | cloc |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions | # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux commands to be executed as a regular non-privileged user |
Install cloc on major Linux distributions
cloc can be installed from your system’s package manager. Use the appropriate command below to install it.
To install cloc on Ubuntu, Debian, and Linux Mint:
$ sudo apt install cloc
To install cloc on Fedora, CentOS, AlmaLinux, and Red Hat:
$ sudo dnf install cloc
To install cloc on Arch Linux and Manjaro:
$ sudo pacman -S cloc
Once it’s installed, you will be able to execute the commands from the examples below.
How to use cloc on Linux
You can use the cloc
command to count the lines of code of an individual file, multiple files, a directory, or even a compressed archive such as a .tar.gz
and .zip
files.
- Counting the lines of a Bash file:
$ cloc countdown.sh 1 text file. 1 unique file. 0 files ignored. github.com/AlDanial/cloc v 1.82 T=0.03 s (34.3 files/s, 2781.5 lines/s) ------------------------------------------------------------------------------- Language files blank comment code ------------------------------------------------------------------------------- Bourne Shell 1 12 0 69 -------------------------------------------------------------------------------
- Counting the lines of multiple PHP files at once:
$ cloc *.php 209 text files. 209 unique files. 0 files ignored. github.com/AlDanial/cloc v 1.82 T=2.93 s (71.4 files/s, 67066.1 lines/s) ------------------------------------------------------------------------------- Language files blank comment code ------------------------------------------------------------------------------- PHP 209 22423 80758 93103 ------------------------------------------------------------------------------- SUM: 209 22423 80758 93103 -------------------------------------------------------------------------------
- In the next example, we will print results for each file separated on each line. This can be done by the use of
--by-file
option:$ cloc --by-file my_project/ 2 text files. 2 unique files. 0 files ignored. http://cloc.sourceforge.net v 1.82 T=0.01 s (149.5 files/s, 448.6 lines/s) -------------------------------------------------------------------------------- File blank comment code -------------------------------------------------------------------------------- my_project/perl.pl 1 0 2 my_project/bash.sh 1 0 2 -------------------------------------------------------------------------------- SUM: 2 0 4 --------------------------------------------------------------------------------
- We can even do cool things like count the number of lines of code in the currently running kernel of our Linux distro.
$ cloc /usr/src/linux-headers-`uname -r` 347 text files. 346 unique files. 8625 files ignored. github.com/AlDanial/cloc v 1.82 T=4.70 s (54.3 files/s, 20714.3 lines/s) -------------------------------------------------------------------------------- Language files blank comment code -------------------------------------------------------------------------------- C 43 4888 3948 30263 Perl 29 3323 2819 16355 C/C++ Header 55 425 463 13876 DOS Batch 33 63 0 3050 Bourne Shell 50 557 913 2603 make 17 641 569 2160 C++ 1 268 66 1581 Python 7 285 411 1121 yacc 2 170 52 1015 Bourne Again Shell 7 182 198 892 lex 2 131 66 767 Glade 1 58 0 603 NAnt script 1 107 0 442 Assembly 4 282 1107 360 D 2 0 0 99 awk 1 9 5 67 -------------------------------------------------------------------------------- SUM: 255 11389 10617 75254 --------------------------------------------------------------------------------
- And here’s one more example where we count the lines of code in an entire WordPress installation, which is all compressed into a
.tar.gz
file. According to cloc, there are a whopping 778,239 lines of code, and it still had no problem counting them all.$ cloc latest.tar.gz 2421 text files. 2353 unique files. 86 files ignored. github.com/AlDanial/cloc v 1.82 T=29.91 s (78.1 files/s, 40656.4 lines/s) ------------------------------------------------------------------------------- Language files blank comment code ------------------------------------------------------------------------------- JavaScript 543 74183 101931 320805 PHP 992 54140 158885 261259 CSS 554 25906 19563 155284 JSON 73 0 0 28413 Sass 155 2790 462 12011 SVG 15 0 0 344 HTML 1 13 0 84 XML 1 6 0 37 Markdown 1 1 0 2 ------------------------------------------------------------------------------- SUM: 2335 157039 280841 778239 -------------------------------------------------------------------------------
cloc has some extra options, which may come in handy in niche scenarios. To see them all, check out the manual page.
$ man cloc
Closing Thoughts
In this tutorial, we saw how to install cloc on major Linux distros, and use the command to count the number of lines of code in one or more files on Linux. cloc is a simple and speedy program, able to process millions of lines of code in just a few seconds. It works on tons of different programming languages, making it useful for almost any type of developer.