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.
  • Posts

    • Tor Browser 15.0.17 by Razvan Serea Protect your privacy. Defend yourself against network surveillance and traffic analysis. Tor is a network of virtual tunnels that allows people and groups to improve their privacy and security on the Internet. The Tor software protects you by bouncing your communications around a distributed network of relays run by volunteers all around the world: it prevents somebody from watching your Internet connection and learning what sites you visit, it prevents the sites you visit from learning your physical location, and it lets you access sites which are blocked. The Tor Browser Bundle lets you use Tor on Windows, Mac OS X, or Linux without needing to install any software. It can run off a USB flash drive, comes with a pre-configured web browser to protect your anonymity, and is self-contained. Tor Browser 15.0.17 changelog: All Platforms Updated Tor to 0.4.9.11 Updated NoScript to 13.6.25.1984 Build System / All Platforms Bug tor-browser-build#41821: Update gpg subkeys for boklm Bug tor-browser-build#41827: Update morgan's keychain with renewed key Download: Tor Browser (64-bit) | Tor Browser (32-bit) | 109.0 MB (Open Source) View: Tor Browser Website | Other Operating Systems Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Very fitting name since AI users have air where there brains should be.
    • Yes, it was amusing at the time because even then dbrand was well known for stealing the designs of products from other companies. That’s what they do.
  • Recent Achievements

    • Reacting Well
      Wakeen1966 earned a badge
      Reacting Well
    • Rookie
      Almohandis went up a rank
      Rookie
    • Apprentice
      jahara21 went up a rank
      Apprentice
    • Reacting Well
      NovaEdgeX earned a badge
      Reacting Well
    • Week One Done
      NovaEdgeX earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      530
    2. 2
      +Edouard
      266
    3. 3
      PsYcHoKiLLa
      148
    4. 4
      Steven P.
      99
    5. 5
      macoman
      56
  • Tell a friend

    Love Neowin? Tell a friend!