How can I count the number of lines in my all my files in this directory?

2

1

I want to count the lines of all files in this directory and all its subdirectories, but exclude directories "public", "modules", and "templates".

How?

Alex

Posted 2011-07-05T06:31:10.497

Reputation: 1 751

Answers

4

 find . -type f |grep -v "^./public" |grep -v "^./modules"|grep -v "^./templates"|xargs cat |wc -l
  1. make a list of all files under current directory with find . -type f
  2. filter out files from "exclude" dirs with grep -v
  3. xargs will read list of files from stdin and pass all files as options to cat.
  4. cat will print all files to stdout
  5. wc will count lines.

If you want to count lines in every file individually, change xargs cat |wc -l to xargs wc -l

osgx

Posted 2011-07-05T06:31:10.497

Reputation: 5 419

Casper's solution is nicer, avoiding the grep -v. – jfg956 – 2011-08-10T18:47:12.613

jfgagne, If you consider his solution as better, upvote it. My solution is easier to understand and to modify - I think it is better to begginner. Casper's needs a deep knowledge of the find command, and my needs only basic options of find, grep, xargs, wc. – osgx – 2011-08-10T19:23:34.443

Also, -print0 is unportable, according to http://pubs.opengroup.org/onlinepubs/009695399/utilities/find.html "Other implementations have added other ways to get around this problem, notably a -print0 primary that wrote filenames with a null byte terminator. This was considered here, but not adopted. "

– osgx – 2011-08-10T19:26:27.827

2

find . -type d \
  \( -path ./public -o -path ./modules -o -path ./templates \) -prune \
  -o -type -f -print0 | xargs -0 wc

Casper

Posted 2011-07-05T06:31:10.497

Reputation: 191

As osgw pointed out, using -print and piping to xargs cat | wc -l might be more portable, but I like avoiding many greps and avoiding going down uselessly in directories. If you do not have read permission on those directories, lots of warning are avoided. +1, and I think it should be the accepted solution. – jfg956 – 2011-08-10T19:46:54.463