Also, `` and $(). Find combined with one of these is great. One of my favorite one-liners (I even figured it out myself :)) is:
cat `find . --name *.java` | wc -lAlso, `` and $(). Find combined with one of these is great. One of my favorite one-liners (I even figured it out myself :)) is:
cat `find . --name *.java` | wc -l wc -l $(find . -name '*.java') find . -name \*.java -exec cat {} + | wc -lAlso, find may or may not split the files it finds into multiple invocations.
find . -name \*.java -print0 | xargs -0 cat | wc -l
Edit: Dammit. Others have already posted this exact version further below. :-(
I just use M-x find-dired and friends these days for most things, admittedly.
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
-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. wc -l **/*.javafind . -mmin -5 -print
zsh also has the 'zargs' command, which works something like xargs, except that it gets its arguments from the command line (often in conjunction with extended globbing) rather than from stdin.
You might also be interested in reading about the "useless uses of cat"[2]
find . -name *.java | xargs cat | wc -l
find . -name *.java -print0 | xargs -0 cat | wc -lThe command line can be a large as available memory (for a single processes). (Although the ratio between command line length and allocated memory is not obvious since it depends on the number of words in the command line.)
find . -name '*.java' -print0 | xargs -0 cat | wc -l --max-args=max-args
-n max-args
Use at most max-args arguments per command
line. Fewer than max-args arguments will be used
if the size (see the -s option) is exceeded,
unless the -x option is given, in which case xargs
will exit.The xargs utility shall limit the command line length such that when the command line is invoked, the combined argument and environment lists (see the exec family of functions in the System Interfaces volume of POSIX.1-2008) shall not exceed {ARG_MAX}-2048 bytes
so you should never run into the ARG_MAX limit.
E.g.: I like dumping temporary files with timestamps:
some-command-to-generate-log > /tmp/log-$( date +%Y%m%d-%H%M%S )
Now, if I want to wrap that in a larger loop -- say, iterating over a number of files or parameters:
command $( expansion one $( expansion 2 ))
Works
With backticks you'd have to do escapes:
command `expansion 1 \`expansion 2\` `
... which gets tedious.
because it requires less typing. The ISO 8601 standard format.
-Is, -Im, -Ih, and -Id allow you to change the displayed resolution.
Bizarrely, this doesn't seem to be documented in my version of date (coreutils 8.10).
That said, I can't find "-I" documented anywhere.
What I like about the timestamp I use is that it's semantic but sorts lexically as well. Though yours does as well. Hrm.
find . -iname '*.java' -exec cat {} + |wc -l
But if you do that, why not just: find . -iname '*.java' -exec wc -l {} +
Which will also print out output of wc -l for each files along with total lines. cat **/*.java | wc -lIt's not as useful as find.