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?
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?
4
find . -type f |grep -v "^./public" |grep -v "^./modules"|grep -v "^./templates"|xargs cat |wc -l
find . -type f
xargs
will read list of files from stdin and pass all files as options to cat
.cat
will print all files to stdoutIf you want to count lines in every file individually, change xargs cat |wc -l
to xargs wc -l
2
find . -type d \
\( -path ./public -o -path ./modules -o -path ./templates \) -prune \
-o -type -f -print0 | xargs -0 wc
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
Casper's solution is nicer, avoiding the
grep -v
. – jfg956 – 2011-08-10T18:47:12.613jfgagne, 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,
– osgx – 2011-08-10T19:26:27.827-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. "