Question

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

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?

 2063  1185420  2063
1 Jan 1970

Solution

 3275

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

2009-08-31
Peter Elespuru

Solution

 600

You can use the cloc utility which is built for this exact purpose. It reports each the amount of lines in each language, together with how many of them are comments, etc. CLOC is available on Linux, Mac and Windows.

Usage and output example:

$ cloc --exclude-lang=DTD,Lua,make,Python .
    2570 text files.
    2200 unique files.
    8654 files ignored.

http://cloc.sourceforge.net v 1.53  T=8.0 s (202.4 files/s, 99198.6 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
JavaScript                    1506          77848         212000         366495
CSS                             56           9671          20147          87695
HTML                            51           1409            151           7480
XML                              6           3088           1383           6222
-------------------------------------------------------------------------------
SUM:                          1619          92016         233681         467892
-------------------------------------------------------------------------------
2011-02-25
simao

Solution

 569

For another one-liner:

( find ./ -name '*.php' -print0 | xargs -0 cat ) | wc -l

It works on names with spaces and only outputs one number.

2009-08-31
Shizzmo

Solution

 459

If using a decently recent version of Bash (or ZSH), it's much simpler:

wc -l **/*.php

In the Bash shell this requires the globstar option to be set, otherwise the ** glob-operator is not recursive. To enable this setting, issue

shopt -s globstar

To make this permanent, add it to one of the initialization files (~/.bashrc, ~/.bash_profile etc.).

2013-02-04
Michael Wild

Solution

 117

On Unix-like systems, there is a tool called cloc which provides code statistics.

I ran in on a random directory in our code base it says:

      59 text files.
      56 unique files.
       5 files ignored.

http://cloc.sourceforge.net v 1.53  T=0.5 s (108.0 files/s, 50180.0 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
C                               36           3060           1431          16359
C/C++ Header                    16            689            393           3032
make                             1             17              9             54
Teamcenter def                   1             10              0             36
-------------------------------------------------------------------------------
SUM:                            54           3776           1833          19481
-------------------------------------------------------------------------------
2014-05-16
Calmarius

Solution

 52

You didn't specify how many files are there or what is the desired output.

This may be what you are looking for:

find . -name '*.php' | xargs wc -l
2009-08-31
Paweł Polewicz

Solution

 43

Yet another variation :)

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

This will give the total sum, instead of file-by-file.

Add . after find to make it work.

2011-08-01
Motiejus Jakštys

Solution

 35

Use find's -exec and awk. Here we go:

find . -type f -exec wc -l {} \; | awk '{ SUM += $0} END { print SUM }'

This snippet finds for all files (-type f). To find by file extension, use -name:

find . -name '*.py' -exec wc -l '{}' \; | awk '{ SUM += $0; } END { print SUM; }'
2013-04-25
jonhattan

Solution

 34

The tool Tokei displays statistics about code in a directory. Tokei will show the number of files, total lines within those files and code, comments, and blanks grouped by language. Tokei is also available on Mac, Linux, and Windows.

An example of the output of Tokei is as follows:

$ tokei
-------------------------------------------------------------------------------
 Language            Files        Lines         Code     Comments       Blanks
-------------------------------------------------------------------------------
 CSS                     2           12           12            0            0
 JavaScript              1          435          404            0           31
 JSON                    3          178          178            0            0
 Markdown                1            9            9            0            0
 Rust                   10          408          259           84           65
 TOML                    3           69           41           17           11
 YAML                    1           30           25            0            5
-------------------------------------------------------------------------------
 Total                  21         1141          928          101          112
-------------------------------------------------------------------------------

Tokei can be installed by following the instructions on the README file in the repository.

2020-04-01
Jade Ellis

Solution

 28

More common and simple as for me, suppose you need to count files of different name extensions (say, also natives):

wc $(find . -type f | egrep "\.(h|c|cpp|php|cc)" )
2012-10-06
sergeych

Solution

 25

POSIX

Unlike most other answers here, these work on any POSIX system, for any number of files, and with any file names (except where noted).


Lines in each file:

find . -name '*.php' -type f -exec wc -l {} \;
# faster, but includes total at end if there are multiple files
find . -name '*.php' -type f -exec wc -l {} +

Lines in each file, sorted by file path

find . -name '*.php' -type f | sort | xargs -L1 wc -l
# for files with spaces or newlines, use the non-standard sort -z
find . -name '*.php' -type f -print0 | sort -z | xargs -0 -L1 wc -l

Lines in each file, sorted by number of lines, descending

find . -name '*.php' -type f -exec wc -l {} \; | sort -nr
# faster, but includes total at end if there are multiple files
find . -name '*.php' -type f -exec wc -l {} + | sort -nr

Total lines in all files

find . -name '*.php' -type f -exec cat {} + | wc -l
2015-08-28
Paul Draper

Solution

 24

There is a little tool called sloccount to count the lines of code in a directory.

It should be noted that it does more than you want as it ignores empty lines/comments, groups the results per programming language and calculates some statistics.

2009-08-31
sebasgo

Solution

 15

You want a simple for loop:

total_count=0
for file in $(find . -name *.php -print)
do
    count=$(wc -l $file)
    let total_count+=count
done
echo "$total_count"
2009-08-31
ennuikiller

Solution

 13

For sources only:

wc `find`

To filter, just use grep:

wc `find | grep .php$`
2011-11-25
kekszumquadrat

Solution

 13

bash tools are always nice to use, but for this purpose it seems to be more efficient to just use a tool that does that. I played with some of the main ones as of 2022, namely cloc (perl), gocloc (go), pygount (python).

Got various results without tweaking them too much. Seems the most accurate and blazingly fast is gocloc.

Example on a small laravel project with a vue frontend:

gocloc

$ ~/go/bin/gocloc /home/jmeyo/project/sequasa
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
JSON                             5              0              0          16800
Vue                             96           1181            137           8993
JavaScript                      37            999            454           7604
PHP                            228           1493           2622           7290
CSS                              2            157             44            877
Sass                             5             72            426            466
XML                             11              0              2            362
Markdown                         2             45              0            111
YAML                             1              0              0             13
Plain Text                       1              0              0              2
-------------------------------------------------------------------------------
TOTAL                          388           3947           3685          42518
-------------------------------------------------------------------------------

cloc

$ cloc /home/jmeyo/project/sequasa
     450 text files.
     433 unique files.                                          
      40 files ignored.

github.com/AlDanial/cloc v 1.90  T=0.24 s (1709.7 files/s, 211837.9 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
JSON                             5              0              0          16800
Vuejs Component                 95           1181            370           8760
JavaScript                      37            999            371           7687
PHP                            180           1313           2600           5321
Blade                           48            180            187           1804
SVG                             27              0              0           1273
CSS                              2            157             44            877
XML                             12              0              2            522
Sass                             5             72            418            474
Markdown                         2             45              0            111
YAML                             4             11             37             53
-------------------------------------------------------------------------------
SUM:                           417           3958           4029          43682
-------------------------------------------------------------------------------

pygcount

$ pygount --format=summary /home/jmeyo/project/sequasa
┏━━━━━━━━━━━━━━━┳━━━━━━━┳━━━━━━━┳━━━━━━━┳━━━━━━━┳━━━━━━━━━┳━━━━━━┓
┃ Language      ┃ Files ┃     % ┃  Code ┃     % ┃ Comment ┃    % ┃
┡━━━━━━━━━━━━━━━╇━━━━━━━╇━━━━━━━╇━━━━━━━╇━━━━━━━╇━━━━━━━━━╇━━━━━━┩
│ JSON          │     5 │   1.0 │ 12760 │  76.0 │       0 │  0.0 │
│ PHP           │   182 │  37.1 │  4052 │  43.8 │    1288 │ 13.9 │
│ JavaScript    │    37 │   7.5 │  3654 │  40.4 │     377 │  4.2 │
│ XML+PHP       │    43 │   8.8 │  1696 │  89.6 │      39 │  2.1 │
│ CSS+Lasso     │     2 │   0.4 │   702 │  65.2 │      44 │  4.1 │
│ SCSS          │     5 │   1.0 │   368 │  38.2 │     419 │ 43.5 │
│ HTML+PHP      │     2 │   0.4 │   171 │  85.5 │       0 │  0.0 │
│ Markdown      │     2 │   0.4 │    86 │  55.1 │       4 │  2.6 │
│ XML           │     1 │   0.2 │    29 │  93.5 │       2 │  6.5 │
│ Text only     │     1 │   0.2 │     2 │ 100.0 │       0 │  0.0 │
│ __unknown__   │   132 │  26.9 │     0 │   0.0 │       0 │  0.0 │
│ __empty__     │     6 │   1.2 │     0 │   0.0 │       0 │  0.0 │
│ __duplicate__ │     6 │   1.2 │     0 │   0.0 │       0 │  0.0 │
│ __binary__    │    67 │  13.6 │     0 │   0.0 │       0 │  0.0 │
├───────────────┼───────┼───────┼───────┼───────┼─────────┼──────┤
│ Sum           │   491 │ 100.0 │ 23520 │  59.7 │    2173 │  5.5 │
└───────────────┴───────┴───────┴───────┴───────┴─────────┴──────┘

Results are mixed, the closest to the reality seems to be the gocloc one, and is also by far the fastest:

  • cloc: 0m0.430s
  • gocloc: 0m0.059s
  • pygcount: 0m39.980s
2022-11-23
Jean-Christophe Meillaud

Solution

 12

A straightforward one that will be fast, will use all the search/filtering power of find, not fail when there are too many files (number arguments overflow), work fine with files with funny symbols in their name, without using xargs, and will not launch a uselessly high number of external commands (thanks to + for find's -exec). Here you go:

find . -name '*.php' -type f -exec cat -- {} + | wc -l
2013-07-10
gniourf_gniourf

Solution

 11

None of the answers so far gets at the problem of filenames with spaces.

Additionally, all that use xargs are subject to fail if the total length of paths in the tree exceeds the shell environment size limit (defaults to a few megabytes in Linux).

Here is one that fixes these problems in a pretty direct manner. The subshell takes care of files with spaces. The awk totals the stream of individual file wc outputs, so it ought never to run out of space. It also restricts the exec to files only (skipping directories):

find . -type f -name '*.php' -exec bash -c 'wc -l "$0"' {} \; | awk '{s+=$1} END {print s}'
2015-04-28
Gene

Solution

 9

I know the question is tagged as , but it seems that the problem you're trying to solve is also PHP related.

Sebastian Bergmann wrote a tool called PHPLOC that does what you want and on top of that provides you with an overview of a project's complexity. This is an example of its report:

Size
  Lines of Code (LOC)                            29047
  Comment Lines of Code (CLOC)                   14022 (48.27%)
  Non-Comment Lines of Code (NCLOC)              15025 (51.73%)
  Logical Lines of Code (LLOC)                    3484 (11.99%)
    Classes                                       3314 (95.12%)
      Average Class Length                          29
      Average Method Length                          4
    Functions                                      153 (4.39%)
      Average Function Length                        1
    Not in classes or functions                     17 (0.49%)

Complexity
  Cyclomatic Complexity / LLOC                    0.51
  Cyclomatic Complexity / Number of Methods       3.37

As you can see, the information provided is a lot more useful from the perspective of a developer, because it can roughly tell you how complex a project is before you start working with it.

2013-10-31
Ja͢ck

Solution

 9

If you want to keep it simple, cut out the middleman and just call wc with all the filenames:

wc -l `find . -name "*.php"`

Or in the modern syntax:

wc -l $(find . -name "*.php")

This works as long as there are no spaces in any of the directory names or filenames. And as long as you don't have tens of thousands of files (modern shells support really long command lines). Your project has 74 files, so you've got plenty of room to grow.

2016-06-08
alexis

Solution

 7

WC -L ? better use GREP -C ^

wc -l? Wrong!

The wc command counts new lines codes, not lines! When the last line in the file does not end with new line code, this will not be counted!

If you still want count lines, use grep -c ^. Full example:

# This example prints line count for all found files
total=0
find /path -type f -name "*.php" | while read FILE; do
     # You see, use 'grep' instead of 'wc'! for properly counting
     count=$(grep -c ^ < "$FILE")
     echo "$FILE has $count lines"
     let total=total+count #in bash, you can convert this for another shell
done
echo TOTAL LINES COUNTED:  $total

Finally, watch out for the wc -l trap (counts enters, not lines!!!)

2013-07-10
Znik

Solution

 5

Giving out the longest files first (ie. maybe these long files need some refactoring love?), and excluding some vendor directories:

 find . -name '*.php' | xargs wc -l | sort -nr | egrep -v "libs|tmp|tests|vendor" | less
2013-07-08
Matt

Solution

 5

For Windows, an easy-and-quick tool is LocMetrics.

2015-01-14
walv

Solution

 5

You can use a utility called codel (link). It's a simple Python module to count lines with colorful formatting.

Installation

pip install codel

Usage

To count lines of C++ files (with .cpp and .h extensions), use:

codel count -e .cpp .h

You can also ignore some files/folder with the .gitignore format:

codel count -e .py -i tests/**

It will ignore all the files in the tests/ folder.

The output looks like:

Long output

You also can shorten the output with the -s flag. It will hide the information of each file and show only information about each extension. The example is below:

Short output

2020-04-17
voilalex

Solution

 4

Very simply:

find /path -type f -name "*.php" | while read FILE
do
    count=$(wc -l < $FILE)
    echo "$FILE has $count lines"
done
2009-09-01
ghostdog74

Solution

 4

If you want your results sorted by number of lines, you can just add | sort or | sort -r (-r for descending order) to the first answer, like so:

find . -name '*.php' | xargs wc -l | sort -r
2012-12-14
Paul Pettengill

Solution

 4

Something different:

wc -l `tree -if --noreport | grep -e'\.php$'`

This works out fine, but you need to have at least one *.php file in the current folder or one of its subfolders, or else wc stalls.

2013-02-02
nav

Solution

 4

While I like the scripts, I prefer this one as it also shows a per-file summary as well as a total:

wc -l `find . -name "*.php"`
2014-10-08
akiva

Solution

 4

It’s very easy with Z shell (zsh) globs:

wc -l ./**/*.php

If you are using Bash, you just need to upgrade. There is absolutely no reason to use Bash.

2018-10-26
HappyFace

Solution

 4

If the files are too many, better to just look for the total line count.

find . -name '*.php' | xargs wc -l | grep -i ' total' | awk '{print $1}'
2019-02-01
bharath

Solution

 3

If you need just the total number of lines in, let's say, your PHP files, you can use very simple one line command even under Windows if you have GnuWin32 installed. Like this:

cat `/gnuwin32/bin/find.exe . -name *.php` | wc -l

You need to specify where exactly is the find.exe otherwise the Windows provided FIND.EXE (from the old DOS-like commands) will be executed, since it is probably before the GnuWin32 in the environment PATH and has different parameters and results.

Please note that in the command above you should use back-quotes, not single quotes.

2011-02-10
Neven Boyanov