How to Count All the Lines of Code in a Directory Recursively in Linux

1. Navigate to the directory containing the code you want to count.

2. Run the following command: find . -name “*.ext” -type f -exec wc -l {} \; | awk ‘{sum+=$1} END {print sum}’

3. Replace “ext” with the file extension of the code you want to count (e.g. “.py” for Python code).

4. The output of the command will be the total number of lines of code in the directory.
[ad_1]

If you want to count the lines of code inside the files of a directory and subdirectory. In this situation do you have to count the lines of code from a file by opening each directory and subdirectory of the file? But for this, some commands have been given in Linux, Unix, and Python systems. By using which you can do this easily.

Count all the lines of code in a directory recursively in linux: In this tutorial, you will learn how to count all the lines of code in a directory recursively.

How to Count All the Lines of Code in a Directory Recursively

By using the following methods, you can count all the lines of code in directory recursively:

  • Method 1: Using the wc command
  • Method 2: Using cloc
  • Method 3: Using a script

Method 1: Using the wc command

Using the simplest way to count the number of lines of code in a directory recursively is by using the wc command. This command is a Unix utility that can be used to count the number of lines, words, and characters in a file.

Firstly, open a terminal window and navigate to the directory, in which you want to count the lines of code in. Then, execute the following wc command on terminal:

$ find . -name "*.py" -or -name "*.js" -or -name "*.html" | xargs wc -l

The above-given command will find/search/get for all files with the .py, .js, and .html extensions in the current directory and its subdirectories and count the number of lines of code in each file.

Method 2: Using cloc

Using the cloc command line, you can count the lines of code current and it’s subdirectories with a variety of programming languages, including C, C++, Java, JavaScript, and Python.

T

Before use the cloc command, you need to install it on your system. You can download the latest version of cloc from its official website (https://github.com/AlDanial/cloc).

Once you have installed cloc in your system. After that, start a terminal window and navigate to the directory, in which you want to count the lines of code in. After that, execute the following cloc command on terminal or command line:

$ cloc .

The above given cloc command is a very simple command for counting the lines of code in all the files in the current directory and its subdirectories and display the results for each programming language.

Method 3: Using a script

If you need more control over the counting process, you can write a script to count the lines of code in a directory recursively. Here is an example script written in Python:

import os

def count_lines_of_code(directory):
    total_lines = 0
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith(".py") or file.endswith(".js") or file.endswith(".html"):
                filepath = os.path.join(root, file)
                with open(filepath, "r") as f:
                    lines = len(f.readlines())
                    total_lines += lines
    return total_lines

directory = "/path/to/your/directory"
lines_of_code = count_lines_of_code(directory)
print(f"Total lines of code in {directory}: {lines_of_code}")

This script uses the os module to traverse the directory recursively and count the lines of code in all the files with the .py, .js, and .html extensions. You can customize the script to count the lines of code in different programming languages by changing the file extensions in the if statement.

Conclusion

Counting the lines of code in a directory recursively can be a challenging task, but with the right tools and techniques, it can be done easily and efficiently. Whether you choose to use the wc command, cloc, or a custom script, tracking the number of lines of code in your project can provide valuable insights into its complexity, maintainability, and overall health.

Recommended Tutorials

[ad_2]

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

GSTIN is 03EGRPS4248R1ZD.

Contact
Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00