Count # of lines of all files in a directory


Recommended Posts

I need to find out the lines of code on a website my school group just developed for our presentation. There are a bunch of directories and subdirectories...

I know if you do wc -l *, it'll print the lines in each file within that ONE directory, as well as the total, however is there a way to do this recursively, so it will get the total number of lines in every file within ever directory?

Thanks in advance...

U can make a shell script that

1. That recieves the names of the directories as arguments

2. stores ur root directory path in some variable say $pth

3. and does a "wc -l *" on $pth/(argument parsed) until no more arguments r there. and adds the result of "wc -l *" to some variable (say $result) recursively!

4. end

Easy:

find . -name *.cpp | xargs wc | tail -n 1 | awk '{ print $1; }'

Find all .cpp files in the current, and sub-directories and total their lines.

An explanation. 'find . -name *.cpp' finds the cpp files. 'xargs wc' takes the file names and makes them command line arguments to wc which counts the lines, words and characters. 'tail -n 1' takes the last line from wc which is the total, and finally, awk takes the first column of output which is the number of lines. Like I said, easy. And remember, TMTOWTDI (the Perl slogan, There's More Than One Way To Do It).

Edited by MrA
...
find . -name *.cpp | xargs wc | tail -n 1 | awk '{ print $1; }'

...

I wish I were that good.

I mean, I can understand that, by reading each part, but I could not just think "I need to do blah..." and then come up with something like that first try at the bash prompt.

I wish I were that good.

Trust me, I'm considered a noob.

EDIT: I just thought of another way to do it. I have waaaay too much time.

find . -name *.cpp -exec wc '{}' ';' | perl -e '$sum = 0; while(<>) { /$[\s]*[0-9]*/; $sum += $_; }; print $sum\n";'

EDIT2: Ok, this is the last way I'll say.

find . -name *.cpp -exec wc '{}' ';' | awk '{ count += $1; } END { print count; }'
Edited by MrA
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.