Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I count all the lines of code in a directory recursively?

Tags:

bash

shell

We've got a PHP application and want to count all the lines of code under a specific directory and its subdirectories.

We don't need to ignore comments, as we're just trying to get a rough idea.

wc -l *.php  

That command works great for a given directory, but it ignores subdirectories. I was thinking the following comment might work, but it is returning 74, which is definitely not the case...

find . -name '*.php' | wc -l 

What's the correct syntax to feed in all the files from a directory resursively?

like image 357
user77413 Avatar asked Aug 31 '09 17:08

user77413


People also ask

How do I count lines of code in a folder?

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.

How do you count all lines in all files in a directory?

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.


1 Answers

Try:

find . -name '*.php' | xargs wc -l 

or (when file names include special characters such as spaces)

find . -name '*.php' | sed 's/.*/"&"/' | xargs  wc -l 

The SLOCCount tool may help as well.

It will give an accurate source lines of code count for whatever hierarchy you point it at, as well as some additional stats.

Sorted output:

find . -name '*.php' | xargs wc -l | sort -nr

like image 60
Peter Elespuru Avatar answered Oct 03 '22 12:10

Peter Elespuru