Skip to main content

How to 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 within a given directory, but ignores subdirectories. I was thinking this 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?


Source: Tips4allCCNA FINAL EXAM

Comments

  1. Try

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


    This may help as well

    http://www.dwheeler.com/sloccount/

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

    ReplyDelete
  2. For another one-liner:

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


    works on names with spaces, only outputs one number

    ReplyDelete
  3. You didn't specify how many files are there or what is the desired output.
    Is this what You are looking for:

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


    ?

    ReplyDelete
  4. There is a little tool called sloccount to count the lines of code in 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.

    ReplyDelete
  5. For everyone stuck with windows:

    After I run into some problems counting lines of code under Windows, I found cloc.

    Serves the same purpose of sloccount but works flawlessly on Windows.

    ReplyDelete
  6. what you want is 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

    ReplyDelete
  7. 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.

    ReplyDelete
  8. Yet another variation :)

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

    ReplyDelete
  9. cat `find -name "*.php"` | wc -l


    should do the trick. This answer has been given again, sorry (missed the other answer link mine)..

    ReplyDelete
  10. very simply

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

    ReplyDelete
  11. cat \`find . -name "*.php"\` | wc -l

    ReplyDelete
  12. for sources only:

    wc `find`


    to filter, just use grep

    wc `find | grep .php$`

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.