Count All the Lines of Code in Directory | Baeldung on Linux (2024)

1. Overview

In this article, we’re going to see how to count the number of lines in the files contained in one directory as well as multiples directories and subdirectories. In addition, we’ll discuss the case of multiples types of files, and we’ll see how to count millions of lines of code.

By way of illustration, we’ve taken projects like wget, WordPress, HAproxy, and the Linux Kernel to demonstrate different use cases. Also, we’re going to take a look at the wc, find and xargs commands. We’ll also pay special attention to how we can combine them to deal with complex files organization.

Furthermore, we’ll see how to use the tools in the correct way to obtain an accurate result.

2. Counting Lines in a File

First, notice that without providing any argument, the wc command counts and prints the number of lines, words, and bytes of each given file passed as a parameter:

$ wc /usr/include/stdio.h870 4202 29660 /usr/include/stdio.h

Secondly, to count and display only the number of lines, we use the -l option or longer –lines option:

$ wc -l /usr/include/stdio.h870 /usr/include/stdio.h

3. Counting Lines in Files Inside a Directory

To illustrate how to count the number of lines inside multiples files contained in one directory, we’ll take the source code of the wget tool. It’s a network downloader utility available under most GNU/Linux distributions. All the source code files are in the src directory, including the header files.

First, let’s start by counting only C language files *.c:

$ wc -l wget-1.21.1/src/*.c 101 src/build_info.c 1084 src/connect.c 1230 src/convert.c ... 95 src/xattr.c 54300 total

Secondly, we can use the tail -1 command to select the last line of the wc output. Because as we’ve seen the wc command prints the number of lines of each .c file:

$ wc -l wget-1.21.1/src/*.c | tail -154300 total

In addition to C language files, we’re going to count the lines of headers files (*.h). To count both types of files, we do:

$ wc -l wget-1.21.1/src/*.[ch] | tail -157241 total

4. Counting Lines of Files in Multiple Directories

To learn how to count the number of lines inside multiples files contained in different directories, we’ve chosen the HAproxy project. Again, its code, C language files *.c can be found inside the src directory. The headers files *.h are split into two sub-directories, include/import and the include/haproxy directory.

So to count the number of lines of the HAproxy source code, we can do:

$ cd haproxy$ find src/ include/ -name '*.[ch]' | xargs wc -l | tail -1256475 total

First, the find command fetches all C language files and header files in the src and include directories, respectively.

Secondly, all files are passed one by one to wc command via xargs. So the wc command will perform the count of the number of lines for each file.

Finally, we select the last line of the output via the tail command. As we can see, it represents the total number of lines for all files.

A common error is to use wc with find without the xargs command. In fact, this will count the number of files which is totally wrong:

$ find src/ include/ -name '*.[ch]' | wc -l |tail -1403

Here 403 is the number of files found not the number of lines inside those files.

4.1. Counting Lines of Files of Multiple Languages in Multiple Directories

In addition to counting the lines inside files in multi-directories, we typically also need to deal with multi-languages situations. To demonstrate this, we can use the WordPress project which has files in PHP, JavaScript, and CSS.

In a similar manner, we can add the -o option to the find command (o for logical Or) because we’ve multiples types of files.

Let’s count the lines of PHP, JavaScript, and CSS files:

$ find WordPress-5.7.2/ -type f -name '*.php' -o -name '*.js' -o -name '*.css' | xargs wc -l | tail -11248529 total

Another way to perform that is by using a command substitution operator. Bash performs the expansion by executing the find command in a subshell and replacing the command substitution with the output of find:

$ wc -l $(find . -type f -name '*.php' -o -name '*.js' -o -name '*.css') | tail -11248529 total

5. Countings Millions of Lines of Files

So far, the methods we’ve used are very useful for small and middle-sized projects, but they cannot give us the correct answer if we use them for a big project.

As an example, the Linux kernel has many directories, subdirectories, and thousand of files. In such a case, the find command splits the list of files into pieces and makes the wc command print a total for each sublist rather than a total for the entire list.

Furthermore, if we try to pass the output of the find command to the wc command as a parameter, the list of files names will be too long. Hence, we’ll exceed the line length limitation of the wc command, and it will give us an error:

$ wc -l $(find linux/ -type f -name '*.[ch]')bash: /usr/bin/wc: Argument list too long

In this situation, the correct way to perform this operation is to use -exec option of the find command. So, it will run the wc command independently for each file. Since it will not figure out the total of all lines of files, we use the awk command to calculate the amount of all columns.

$ find linux/ -type f -name '*.[ch]' -exec wc -l {} \; | awk '{ total += $1 } END {print total}'28085235

As we can see, the Linux Kernel has more than 28 million lines of code!

6. Conclusion

In this article, we’ve seen how to use Linux tools in a simple manner to efficiently count the number of lines inside files for projects organized in different ways. We’ve seen examples using one directory, multi directories, multi-type, and millions of lines.

Of course, we can learn more about the wc, find andxargs tools by reading their manuals pages.

Count All the Lines of Code in Directory | Baeldung on Linux (2024)

FAQs

Count All the Lines of Code in Directory | Baeldung on Linux? ›

wc

wc
wc (short for word count) is a command in Unix, Plan 9, Inferno, and Unix-like operating systems. The program reads either standard input or a list of computer files and generates one or more of the following statistics: newline count, word count, and byte count.
https://en.wikipedia.org › wiki › Wc_(Unix)
. The wc command is used to find the number of lines, characters, words, and bytes of a file. To find the number of lines using wc, we add the -l option. This will give us the total number of lines and the name of the file.

How do I count the number of lines in a directory in Linux? ›

How to Count the Number of Lines in a Linux File
  1. Open Terminal. Locate the terminal icon on your desktop or search for it in the application menu and open it.
  2. Navigate to the Directory. ...
  3. Use the wc command. ...
  4. Use the grep command. ...
  5. Redirect Output to a File.

How do you count lines of code in Linux? ›

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.

How do I count all items in a directory in Linux? ›

On Linux, the ls command, piped with the wc -l command, is the simplest way to count files in a directory. The wc command is used in Linux to output the number of bytes, characters, or newlines. In this situation, though, we are using this command to count the number of files in a directory.

How do I find the number of lines in all files in a directory? ›

Secondly, all files are passed one by one to wc command via xargs. So the wc command will perform the count of the number of lines for each file. Finally, we select the last line of the output via the tail command. As we can see, it represents the total number of lines for all files.

How do you count lines of code? ›

Counting lines of code manually involves going through each line of code and tallying the lines while excluding comments, blank spaces, and non-executable lines. To do this, open the source code file and count the lines with a paper or text editor. Go through it line by line and check for executable code.

How do you calculate total lines of code? ›

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. Now lets run cloc on it. As you can see it counted the number of files, blank lines, comments and lines of code.

Which Linux command is used to count the total number of lines words? ›

You can use the wc command to count a file's word, line, character, or byte count. But those aren't the only system tasks it can handle. The Linux wc command calculates a file's word, line, character, or byte count.

How to count line CSV in Linux? ›

How do I count the number of lines in a file in Linux? The command is 'wc' you are looking for, if you are looking for simple solution. The command wc basically stands for word count, but with '-l' flag, you can use it to count lines. Similar to other Linux commands, wc uses stdin if no filename is specified.

How do I count the number of lines and words in Linux? ›

The followings are the options and usage provided by the wc command.
  1. wc -l – Prints the number of lines in a file.
  2. wc -w – prints the number of words in a file.
  3. wc -c – Displays the count of bytes in a file.
  4. wc -m – prints the count of characters from a file.
  5. wc -L – prints only the length of the longest line in a file.
Jul 14, 2023

How do you count words in a directory in Linux? ›

Use the wc command to display a count of the lines, words, and characters in a file. The wc command reads one or more input files and, by default, writes the number of newline characters, words, and bytes contained in each input file to the standard output.

What is the maximum directory count in Linux? ›

There's no limit on directories in particular beyond this; keep in mind that every file or directory requires at least one filesystem block (typically 4KB), though, even if it's a directory with only a single item in it. As you can see, though, 80,000 inodes is unlikely to be a problem.

How do I count files in a directory in Linux script? ›

dir="/path/to/your/directory" : This line defines the directory you want to count files in. Replace "/path/to/your/directory" with the path to the directory you want to count files in. count=$(find "$dir" -type f | wc -l) : This line uses the find command to list all files in the directory specified by $dir .

How do I count the number of lines in a directory in Unix? ›

The 'wc -l' command is the most common and straightforward way to count lines in a file using Bash. 'wc' stands for 'word count', but this command can do more than just count words. It can also count lines, characters, and bytes.

What is the command to count the number of lines in a directory? ›

Use wc , originally for word count, I believe, but it can do lines, words, characters, bytes, and the longest line length. The -l option tells it to count lines. How many lines are in directory. BTW, wc command counts new lines codes, not lines.

How do you get the total number of lines in a file in Linux? ›

To count the number of characters, line breaks, and so on, use the wc −l, wc −w, and wc −c commands respectively. We use the wc command to count the lines in a file. Adding the −l flag gives us the total line count and the filename. We can use the wc −l (word count) command to see how many lines there are in our file.

Top Articles
Latest Posts
Article information

Author: Clemencia Bogisich Ret

Last Updated:

Views: 5495

Rating: 5 / 5 (60 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Clemencia Bogisich Ret

Birthday: 2001-07-17

Address: Suite 794 53887 Geri Spring, West Cristentown, KY 54855

Phone: +5934435460663

Job: Central Hospitality Director

Hobby: Yoga, Electronics, Rafting, Lockpicking, Inline skating, Puzzles, scrapbook

Introduction: My name is Clemencia Bogisich Ret, I am a super, outstanding, graceful, friendly, vast, comfortable, agreeable person who loves writing and wants to share my knowledge and understanding with you.