3 ms·
It's also wrong irregardless of the shell escaping. That will not always count the number of lines in all the files under a directory that contain at least one
by mzs 12y ago
It's also wrong irregardless of the shell escaping. That will not always count the number of lines in all the files under a directory that contain at least one "foo" - when shelling-out, you have to be sure to use the tools correctly:
$ mkdir sh
$ cd sh
$ >foo find . -type f -print0
$ >bar head -1 foo foo
$ # four lines with "foo" in file bar
$ >foo echo foo
$ mv foo 'bar
> baz'
$ # one line with "foo" in file bar?baz
$ find . -type f -print0 | xargs -0 grep foo | wc -l
3
$ # Wait a minute that's not five, I KNOW there five lines!
$ find . -type f -print0 | xargs -0 grep foo
Binary file ./bar matches
./bar
baz:foo
$ # oh...
$ find . -type f -exec grep -c foo '{}' \;
4
1
$ # might as well get everything closer to correct
$ env -i /usr/bin/find . -type f -execdir /usr/bin/grep -c foo -- '{}' \; | \
env -i /usr/bin/awk '{t+=$1}END{print t}'
5