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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex