1

This question already has an answer here:

I am using Mac OS terminal (similar to Linux) and trying to find best way to search inside all files on a computer that has extension *.py

What is the best way to achieve this?

I wanted to put 1 keyword for search and quickly show the whole path of these python files are that contain requested keyword in them..

marked as duplicate by Mikel, muru, Jeff Schaller, schily, RalfFriedl Dec 11 '18 at 18:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4

Try this,

find / -name '*.py' -exec grep -l "keyword" {} \;

Explanation:

  • find / -name '*.py': Find files below / with py extension.
  • -exec grep -l keyword {} \; Within the files found, grep for keyword and output the filename instead of the match -l.

I'm not familiar with Mac OS, but if you have globstar option in your shell, you can use the following:

shopt -s globstar
grep -l keyword /**/*.py
  • I think mac has bash shell (same as Gnu/Linux), but by default not all gnu tools (some are bsd). Your find finds files ending .py (there is no such thing as file-name-extensions, on Unix). – ctrl-alt-delor Dec 11 '18 at 15:54
0

To look for the string needle in a bunch of Python files in a haystack directory:

$ grep -l 'needle' haystack/*.py

To descend into a directory tree, you could either use grep -R or switch to iterating via find as in the answer from RoVo.

Not the answer you're looking for? Browse other questions tagged or ask your own question.