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 imagine that was a review or something? My reviews mostly contain a lot of images and galleries, but these are all webp too, but yeah it all adds up on the page load. Would help if you were more helpful with your critique instead of bitching and moaning like a Karen 😂 Because then we might be able to fix it for you.
    • If Valve refused to let them make the case, I wonder if they've already partnered with someone else to do it? The fact that they didn't seek permission/licence before diving straight in is incredible though
    • OpenClaw now has native mobile apps on iOS and Android by Karthik Mudaliar OpenClaw, the viral open-source personal AI agent, now has its own mobile app, available on both Android and iOS. Users can pair the app with an existing OpenClaw gateway and can start using new mobile-native features that are now available on the app. The app supports all the existing features you'd already have seen on OpenClaw's TUI, as well as some more, such as real-time and background Talk mode, action approvals, sharing from iOS, and optional access to device capabilities such as camera, screen, location, photos, contacts, calendar, and reminders. These features are available on both the Android and iOS versions of the app. What's important with these apps is that they don't run OpenClaw on your phone, but are actually just companion apps that require a running OpenClaw Gateway on an existing device, on macOS, Linux, or Windows via WSL2. To pair the app with your existing OpenClaw gateway, users need to run the command "/pair qr" on the TUI or existing chat interface, which brings up a QR code. Users can then scan this QR code to pair it up with the mobile app. There's also an option to manually pair the app by entering the host and a port. Previously, OpenClaw had been available on phones via WhatsApp, Telegram, Slack, Discord, Microsoft Teams, Matrix, and others. Now, with a native mobile app, the interface is much cleaner and more focused on just the OpenClaw, of course, with the added support for camera, screen, location, and more. It's important to note that OpenClaw comes with its own security warnings. There's always a chance of prompt injection with these tools, so users are recommended to double-check authentication, tool policy, sandboxing, and execution approvals rather than prompts alone. For users well-versed with the AI harness, a native mobile app makes it easier to approve an automation, share a link, use voice, or let an agent react to phone-side context.
    • Google pitches Spanner as one database for all AI agents with these new featues by Karthik Mudaliar Google Cloud is introducing new features within Spanner, its distributed database, as a place where enterprises should keep their data, using which AI agents could make smarter and better decisions. In a detailed blog post, Google highlighted quite a few features coming to Spanner, including relational data, graph relationships, vector search, key-value access, full-text search, and operational analytics together in one database architecture. Google says that today's systems aren't well-made for AI agents. There could be data that is present in one system, search indexes in another, embeddings in a vector database, and relationship data in a graph database. This fragmentation isn't great for AI agents to do their jobs because they don't have access to all of this data in one place. This is where Google is positioning Spanner as a solution. Spanner is already a globally distributed relational database with strong consistency, and Google wants its customers to see it as a broader data layer for AI applications. The company introduced something called Spanner Graph, along with integrated vector search, full-text search, a Cassandra-compatible key-value endpoint, and a columnar engine for analytical queries on operational data. Google also added that its ScaNN-powered vector search can support indexes with more than 10 billion vectors, while the columnar engine can make some analytical scans up to 200 times faster. All of this isn't just exclusive to the Google Cloud Platform, and there's support for multi-cloud as well. This comes via Spanner Omni, which Google says is a downloadable, containerized version of Spanner that can run on Kubernetes and in environments outside Google Cloud, including Microsoft Azure and AWS, and even on-premises infrastructure as well as edge deployments. Google says that customers who are interested in the full-featured edition should contact the company, and there's no word on commercial availability or separate pricing. Those interested can read the full blog by Google Cloud, which details these features individually.
  • Recent Achievements

    • First Post
      rosiecharles earned a badge
      First Post
    • Reacting Well
      Juan Dela earned a badge
      Reacting Well
    • Week One Done
      Collagen Project earned a badge
      Week One Done
    • Reacting Well
      Wakeen1966 earned a badge
      Reacting Well
    • Rookie
      Almohandis went up a rank
      Rookie
  • Popular Contributors

    1. 1
      +primortal
      515
    2. 2
      +Edouard
      273
    3. 3
      PsYcHoKiLLa
      143
    4. 4
      Steven P.
      99
    5. 5
      macoman
      54
  • Tell a friend

    Love Neowin? Tell a friend!