I am running -> "find . -name '*.txt'" command and getting list of files.
I am getting below mention output:
./bsd/contrib/amd/ldap-id.txt
./bsd/contrib/expat/tests/benchmark/README.txt
./bsd/contrib/expat/tests/README.txt
./bsd/lib/libc/softfloat/README.txt
and so on,
Out of these files how can i run grep command and read contents and filter only those files which have certain keyword? for e.g. "version" in it.
xargs
is a great way to accomplish this, and its already been covered.
The -exec
option of find
is also useful for this. It will perform a command over all files returned from find.
To invoke grep
as few times as possible, passing multiple filenames to each call:
find . -name '*.txt' -exec grep -H 'foo' {} +
Alternately, to invoke grep
exactly once for each file found:
find . -name '*.txt' -exec grep -H 'foo' {} ';'
In either case, {}
is like a placeholder for the values from find
; if your shell is zsh, it may be necessary to escape it, as in '{}'
.