Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 85569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:06:36+00:00 2026-05-10T22:06:36+00:00

Suppose I want to count the lines of code in a project. If all

  • 0

Suppose I want to count the lines of code in a project. If all of the files are in the same directory I can execute:

cat * | wc -l 

However, if there are sub-directories, this doesn’t work. For this to work cat would have to have a recursive mode. I suspect this might be a job for xargs, but I wonder if there is a more elegant solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:06:37+00:00Added an answer on May 10, 2026 at 10:06 pm

    First you do not need to use cat to count lines. This is an antipattern called Useless Use of Cat (UUoC). To count lines in files in the current directory, use wc:

    wc -l *  

    Then the find command recurses the sub-directories:

    find . -name '*.c' -exec wc -l {} \; 
    • . is the name of the top directory to start searching from

    • -name '*.c' is the pattern of the file you’re interested in

    • -exec gives a command to be executed

    • {} is the result of the find command to be passed to the command (here wc-l)

    • \; indicates the end of the command

    This command produces a list of all files found with their line count, if you want to have the sum for all the files found, you can use find to list the files (with the -print option) and than use xargs to pass this list as argument to wc-l.

    find . -name '*.c' -print | xargs wc -l  

    EDIT to address Robert Gamble comment (thanks): if you have spaces or newlines (!) in file names, then you have to use -print0 option instead of -print and xargs -null so that the list of file names are exchanged with null-terminated strings.

    find . -name '*.c' -print0 | xargs -0 wc -l 

    The Unix philosophy is to have tools that do one thing only, and do it well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I want to execute code, for example value += 5 inside a namespace
I want to count the number of common lines that exist between 2 files
suppose i have what i want is inside maindiv i want to count no
Suppose we want to loop through all the items in a dropdown list and
I have C# list with lot of similar name i want to count all
Ok so iv got this code which is suppose to bring back all my
I have a text file that contains two lines of numbers, all I want
I want to count the number of items of all the levels which are
Suppose I want to add two buffers and store the result. Both buffers are
Suppose I want to store N samples (each sample takes up a significant portion

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.