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: Tips4all
For another one-liner:
ReplyDelete( find ./ -name '*.php' -print0 | xargs -0 cat ) | wc -l
works on names with spaces, only outputs one number
You didn't specify how many files are there or what is the desired output.
ReplyDeleteIs this what You are looking for:
find . -name '*.php' | xargs wc -l
?
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.
ReplyDeleteFor everyone stuck with windows:
ReplyDeleteAfter 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.
what you want is a simple for loop:
ReplyDeletetotal_count=0
for file in $(find . -name *.php -print)
do
count=$(wc -l $file)
let total_count+=count
done
echo $total_count
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:
ReplyDeletecat `/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.
Yet another variation :)
ReplyDelete$ find -name '*.php' | xargs cat | wc -l
cat `find -name "*.php"` | wc -l
ReplyDeleteshould do the trick. This answer has been given again, sorry (missed the other answer link mine)..
very simply
ReplyDeletefind /path -type f -name "*.php" | while read FILE
do
count=$(wc -l < $FILE)
echo "$FILE has $count lines"
done
cat \`find . -name "*.php"\` | wc -l
ReplyDeletefor sources only:
ReplyDeletewc `find`
to filter, just use grep
wc `find | grep .php$`