count number of lines in directory in linux

How to Count Total Lines of Code in Directory Recursively

Sometimes you may need to count total lines of code in your application or website. For this purpose, you can cloc (count lines of code) tool in Linux. It is a useful utility that is available for all Linux distributions and supports all major programming languages and does not have any major dependencies so it can be easily installed on all Linux systems. In this article, we will learn how to count total lines of code in directory recursively.


How to Count Total Lines of Code in Directory Recursively

Here are the steps to count total lines of code in directory recursively.


1. Install cloc

Open terminal and run the following command, depending on your system to install cloc.

$ 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


2. Use cloc

You can use cloc to count number of lines in a single file, multiple files as well as folders. You just need to mention the file/folder name after cloc command. Let us say you have the following file.

$ vi test.sh

Add the following lines to it.

Run the cloc command to count the file’s lines.

$ cloc test.sh

You will see the following output. It will show the total number of lines, files, blanks and comments in your file.

count number of lines in directory

It even works on compressed files such as .tar.gz files.

$ cloc latest.tar.gz

You will see the following output.

In this case, it will list the number of lines of code by language, and provides stats per language.

If you need stats for multiple files, you can run cloc command using –by-file option which will count the lines in each file and provide detailed stats about each file. Its syntax is as follows.

$ cloc --by-file <directory>

Here is an example to count number of lines in files in directory.

In this case, you will see that cloc counts and displays number of lines in each file.

cloc command provides many useful features that you can learn by referring to its man pages.

$ man cloc

Here are some commonly used options in cloc.

--ignore-whitespace – ignore horizontal whitespace when comparing files with --diff.
--max-file-size=<MB> – to skip files larger than the given amount MB.
--exclude-dir=<dir1>,<dir2> – exclude given comma separated directories.
--exclude-ext=<ext1>,<ext2> – exclude given file extensions.
--csv – export results to CSV file format.

--out=<file> – save the results to <file>.
--quiet – suppress all information messages and show only final report.

In this article, we have learnt how to count number of lines in directory using cloc command.

There are many tools to count number of lines in files and folders but cloc is command line tool so you can easily add it to a shell script to automate counting of lines in directory. Also, it supports wide range of programming languages making it easy to use on even large projects which contain different languages. It also works on compressed files and also provides detailed report for each file.

Also read:

How to Fix Yum Error: Database Disk Image is Malformed
How to Fix ‘Failed to Mount /etc/fstab” Error in Linux
How to Compress Images in Linux
How to Reduce PDF Size in Linux
How to Password Protect PDF in Linux

Leave a Reply

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