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

    • Free Software Foundation Europe pushes EU to force Google to allow AI uninstalls on Android by Paul Hill Credit: Pexels Users should be able to fully uninstall AI-based features from Android devices and be able to access interoperability functions, free from Google’s verification requirements, the European Commission has heard as part of an Android interoperability consultation under the Digital Markets Act. These measures were proposed by the Free Software Foundation Europe (FSFE) last week when it submitted its documentation. The FSFE noted that Google had started silently installing AI models without telling users. It noted that the EU’s DMA requires companies like Google to allow users to uninstall pre-loaded software from their devices, but in the case of the AI models Google is installing, they reinstall if you delete them, contravening the DMA. To get Google back under control, the FSFE has told the European Commission that there needs to be improvements within the Android Open Source Project (AOSP). First, it said that users should be able to fully remove pre-loaded AI components from their devices, with companies being prohibited from silently reinstalling or reactivating them. Second, access to Android interoperability features should not be contingent on registration, authorization, or contractual relationships with Google. This pertains to Google’s attempt to force developers to register with Google, even to publish apps to alternative app stores like F-Droid. Discussing its submission, Lucas Lasota, FSFE Legal Programme Manager, said: Google is planning to roll out its Android Developer Certification in September 2026. This will force every Android app developer to register with Google before their software can be installed on certified Android devices, but it should affect those who have removed Google Apps from their device. The program is controversial because it entails the signing of contracts and payment of account fees to Google, as well as the handing over of the identities of developers. It said: The FSFE said that if the Commission’s draft measures remain unchanged, then Google will be allowed to make developers verify their identity. The FSFE believes that asking developers to register is contrary to the text and spirit of the law. In summary, the FSFE has told the Commission that no developer should need a Google account, a Play Store presence, or any agreement with Google to access Android’s interoperability features.
    • You could disable this "functionality/feature/whatever" with 2 registry keys, ages ago.
    • My Photos app is version 2026.11050.1001.0 and it remembers the window size and position. My Snipping Tool is version 11.2602.49.0 and it can capture the taskbar.
  • Recent Achievements

    • Conversation Starter
      sumytbe earned a badge
      Conversation Starter
    • One Year In
      B4dM1k3 earned a badge
      One Year In
    • One Year In
      DarkWun earned a badge
      One Year In
    • Dedicated
      Almohandis earned a badge
      Dedicated
    • Dedicated
      JuvenileDelinquent earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      519
    2. 2
      +Edouard
      187
    3. 3
      PsYcHoKiLLa
      87
    4. 4
      Michael Scrip
      81
    5. 5
      Steven P.
      72
  • Tell a friend

    Love Neowin? Tell a friend!