如何仅查找存在目标文本的 py 文件名?

How to find only py file names in which target text is present?

我正在使用CentOS
我必须在当前目录 /var/opt

的仅 .py 个文件中找到 /tmp 文本

我尝试跟随,这是给我所有文件,即 pypyctext,其中 /tmp 找到。

$ pwd
/var/opt
$ grep -r "/tmp" * >>/tmp/241_tmp.txt

以下无效:

$ grep /tmp *.py 
grep: *.py: No such file or directory

谢谢。

如果您只想搜索 .py 文件,请不要搜索所有文件:

grep /tmp *.py

如果您的当前目录不同,请指定路径:

grep /tmp /var/opt/*.py

您可以使用 --include 参数:

grep -r --include \*.py "/tmp" /var/opt

请注意,您需要使用反斜杠转义 * 符号。

还有一个 --exclude 参数,如果您需要,可以使用它,否则 - 除了搜索中的某些文件(如 *.pyc)。