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

    • I agree when are you going to read this (really poor BTW) article?
    • I disagree; they come off very "bitchy" and "whiny". Make a great product and combine that with a great price (free) and people will come over to your side. Or build it and they will come as they say. Constantly trying to get attention by complaining all the time, will turn people off to your product.
    • It use to be a nightmare, with LibreOffice supporting a newer draft ODF standard by default, and Microsoft Office supporting the older non-draft standard. Now that they both support the same version of ODF, they should be interoperable.
    • Brave Browser 1.91.171 by Razvan Serea Brave Browser is a lightning-fast, secure web browser that stands out from the competition with its focus on privacy, security, and speed. With features like HTTPS Everywhere and built-in tracker blocking, Brave keeps your online activities safe from prying eyes. Brave is one of the safest browsers on the market today. It blocks third-party data storage. It protects from browser fingerprinting. And it does all this by default. Speed - Brave is built on Chromium, the same technology that powers Google Chrome, and is optimized for speed, providing a fast and responsive browsing experience. Brave Browser also features Brave Rewards, a system that rewards users with Basic Attention Tokens (BAT) for viewing opt-in ads. This innovative system provides an alternative revenue model for content creators and a way to support the Brave community. SlimBrave Neo takes all the good things about Brave and makes them even better by keeping everything clean, light, and privacy-focused. It removes the extra clutter, turns off features you might not need, and cuts down on anything that could slow you down or collect unnecessary data. Because it relies on simple settings and policies instead of modifying the browser itself, you still get full Brave compatibility—just in a smoother, lighter, and more privacy-friendly package. Brave Browser 1.91.171 changelog: General Fixed Cardano not being disabled on upgrade to Brave Origin. Upgraded Chromium to 149.0.7827.103. Origin Removed “Survey Panelist” setting from brave://settings/privacy. Fixed P3A and usage ping under brave://settings/privacy being displayed on first launch on Linux. Upgraded Chromium to 149.0.7827.103. Download: Brave Browser 64-bit | 1.2 MB (Freeware) Download: Brave Browser 32-bit View: Brave Homepage | Offline Installers | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Hi. As the title suggests, I can't access the forum on my phone. I'm using Edge on Android and when I try to navigate to the forum I get a "we value your privacy" popup and none of the buttons are clickable. It effectively stonewalls me from reading any forum content.
  • Recent Achievements

    • Rookie
      Marzoid went up a rank
      Rookie
    • Community Regular
      coch went up a rank
      Community Regular
    • One Year In
      slackerzz earned a badge
      One Year In
    • One Year In
      highriskpaym earned a badge
      One Year In
    • One Month Later
      highriskpaym earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      519
    2. 2
      PsYcHoKiLLa
      190
    3. 3
      +Edouard
      156
    4. 4
      Steven P.
      84
    5. 5
      ATLien_0
      75
  • Tell a friend

    Love Neowin? Tell a friend!