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

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

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

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

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

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 3699

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T05:09:07+00:00 2024-11-26T05:09:07+00:00

How to count lines of code

  • 61k

This post if a followup for the previous one, on how to count lines in text or in a in a text file in Javascript. In this post I'm going to show how to count the lines of code in a project directory or any directory containing source code files of any type.

Using find, wc and xargs

In unix-linux based systems we can use find wc and xargs to count lines in files based on their extension.

find . -name '*.py' | xargs wc -l 
Enter fullscreen mode Exit fullscreen mode

Excluding directories:

find . -name '*.py' -not -path './exclude-dir/*' | xargs wc -l 
Enter fullscreen mode Exit fullscreen mode

Handling Spaces in Filenames

If the files or directories have spaces in their names, it’s safer to use find … -print0 with xargs -0 to handle these names correctly.

find . -name '*.py' -print0 | xargs -0 wc -l 
Enter fullscreen mode Exit fullscreen mode

  • find . -name '*.py' -print0: This finds all files ending with .py starting from the current directory (.). It prints the list of found files with each filename followed by a null character () instead of a newline. This null character handling allows for filenames with special characters (including spaces) to be processed correctly by xargs.
  • xargs -0 wc -l: The -0 option tells xargs to expect null-terminated strings as input (matching the output of find … -print0). xargs passes the filenames to wc -l, which counts the lines in each file. The output is a list of line counts followed by the corresponding filename.

Counting Lines for Multiple File Types

find . ( -name '*.py' -o -name '*.js' ) -print0 | xargs -0 wc -l 
Enter fullscreen mode Exit fullscreen mode

Summing Total Lines

find . -name '*.py' | xargs wc -l | tail -n 1 
Enter fullscreen mode Exit fullscreen mode

Using awk to “accumulate” line count

find . -name '*.py' -print0 | xargs -0 wc -l | awk '{total += $1} END {print total}' 
Enter fullscreen mode Exit fullscreen mode

awk '{total += $1} END {print total}': This awk script sums up the line counts. It reads the line counts (which are the first whitespace-delimited field $1 in the wc -l output) and accumulates them in the total variable. After processing all input lines, the END block prints the total sum of lines.

Using cloc(Count Lines of Code)

cloc (Count Lines of Code) is a specialized command line tool that not only counts lines of text, but also distinguishes between code, comments, and blank lines across multiple programming languages.

cloc . 
Enter fullscreen mode Exit fullscreen mode

github.com/AlDanial/cloc v 1.82  T=2.12 s (1500.2 files/s, 240327.6 lines/s) ------------------------------------------------------------------------------- Language                     files          blank        comment           code ------------------------------------------------------------------------------- JavaScript                    2257          39250          72045         199230 JSON                           229              9              0          39756 Markdown                       308          13894              2          39106 CSS                             21           2721            151          36892 Python                          49           4343           7763          21745 C/C++ Header                    10           1465            916           8019 Sass                            88           1339           1297           6797 TypeScript                     110            451           1794           2616 C++                              6            392             99           1840 Handlebars                      36            424             81           1184 HTML                             5              5              0            692 YAML                            33             37             34            567 SVG                              1              0            101            500 Lisp                             2             42             38            258 C#                               1             55              9            186 XML                              5              0              0             72 SQL                              2             11             16             66 make                             5             25              4             60 Bourne Shell                     3              4              6             26 Nix                              1              1              0             19 DOS Batch                        2              1              0              6 ------------------------------------------------------------------------------- SUM:                          3174          64469          84356         359637 ------------------------------------------------------------------------------- 
Enter fullscreen mode Exit fullscreen mode

Advanced Options

Filter by Specific Languages:

cloc --include-lang=Python,JavaScript .     3843 text files.     3510 unique files.                                               1553 files ignored.  github.com/AlDanial/cloc v 1.82  T=1.33 s (1734.4 files/s, 259007.6 lines/s) ------------------------------------------------------------------------------- Language                     files          blank        comment           code ------------------------------------------------------------------------------- JavaScript                    2257          39250          72045         199230 Python                          49           4343           7763          21745 ------------------------------------------------------------------------------- SUM:                          2306          43593          79808         220975 ------------------------------------------------------------------------------- 
Enter fullscreen mode Exit fullscreen mode

Exclude Directories:

cloc --exclude-dir=node_modules .      131 text files.      117 unique files.                                                 49 files ignored.  github.com/AlDanial/cloc v 1.82  T=0.14 s (673.5 files/s, 480769.4 lines/s) ------------------------------------------------------------------------------- Language                     files          blank        comment           code ------------------------------------------------------------------------------- CSS                             18           2707            145          36722 JavaScript                      29           4703           1679          13994 JSON                             4              0              0           3862 Handlebars                      36            424             81           1184 SQL                              2             11             16             66 Markdown                         3             20              0             63 ------------------------------------------------------------------------------- SUM:                            92           7865           1921          55891 ------------------------------------------------------------------------------- 
Enter fullscreen mode Exit fullscreen mode

If you liked this post, give a thumb up and check my other post on how to count the number lines in a text or text files in javascript, or use my online tool to count lines in a text.

🚀 Interested to learn more 🛠️🤖💻? Then don't forget to 📬✉️, 👇

🚀 If you are interested in learning more about programming, 🛠️ building applications, or in general about AI 🤖 and tech 💻, you can subscribe to my newsletter at websilvercraft.substack.com ✉️ to get the posts delivered directly to you as soon as I publish them! 📬
⠀⠀⠀ ⠀⠀⠀⠀ ⠀⠀ ⠀👆👆👆👆
⠀⠀⠀⠀⠀⠀⠀⠀ 👆👆👆👆👆👆👆👆👆👆👆
⠀ ⠀ 👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆
👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆

bashbeginnersjavascriptwebdev
  • 0 0 Answers
  • 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

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

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

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

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.