Hacker News new | ask | show | jobs
by thelastnode 5325 days ago
For find, you need to use single quotes so your shell doesn't automatically expand the .java glob into the names of all the files. Also, find uses one dash for parameters, and can take an exec parameter: find . -name .java -exec wc -l {} \;

The {} means "replace with filename" and the semicolon (escaped so your shell doesn't eat it). This will run wc -l <filename> for every file that matches.

The -regex flag lets you do the same thing as -name with regular expressions.

The problem with this is we run n different wc's, as opposed to just one with all the parameters. We can use xargs to fix this: find . -name .java | xargs wc -l

This even gives you a total!

The really* simple solution (which is probably best) is to just use shell globbing: wc -l *.java

While find and xargs are definitely useful, the easy solution here works better

1 comments

You can skip xargs altogether with the plus sign instead of semicolon.

    -exec utility [argument ...] {} +
            Same as -exec, except that ``{}'' is replaced with as many path-
            names as possible for each invocation of utility.  This behaviour
            is similar to that of xargs(1).
The problem with just wc -l *.java is that it don't recursive directory.
With bash-4 or zsh

  wc -l **/*.java