If I understand you correctly this is what you want to do:
find . -name '*.py' -print0 | xargs -0 grep 'something' > output.txt
Find all files with extension .py
, grep
only rows that contain something
and save the rows in output.txt
. If the output.txt
file exists, it will be truncated, otherwise it will be created.
Using -exec
:
find . -name '*.py' -exec grep 'something' {} \; > output.txt
I'm incorporating Chris Downs comment here: The above command will result in grep
being executed as many times as find
finds pathnames that passes the given tests (only the single -name
test above). However, if you replace the \;
with a +
, grep
is called with multiple pathnames from find
(up to a certain limit).
See question Using semicolon (;) vs plus (+) with exec in find for more on the subject.