5 ms·
Never thought to use ``. I always used xargs: find . -name *.java | xargs cat | wc -l
by sbov 15y ago
Never thought to use ``. I always used xargs:
find . -name *.java | xargs cat | wc -l
- cperciva 15y agoYour approach works better than backtick expansion, since it won't run into 'command line too long' issues; but it will still break on "weird" file names (e.g., file names with spaces in them). The correct way to do this is find . -name *.java -print0 | xargs -0 cat | wc -l
- ars 15y agoBTW, recent versions of linux don't have a fixed limit on command line length. The 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.)
- ralph 15y agoThat glob could cause trouble. find . -name '*.java' -print0 | xargs -0 cat | wc -l
- pyre 15y agoxargs can still run you into the 'too many args' issue. You'll want to use this: --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.
- cperciva 15y agoOnly if there's a bug in your xargs. POSIX states: 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.
- sirn 15y agoWithout xargs: 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.
- dredmorbius 15y agoDollar-paren expansion beats backticks for being nestable. 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.
- jbarber 15y agoI like to use (for GNU date at least): $ date -Iseconds 2011-11-21T10:56:17+0000 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).
- dredmorbius 15y agoA lot of the date documentation is unfortunately in info only. Debian (and derived distros) do a good job of updating the manpages. 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.