grep -- 列出找到匹配项的文件名

BeM*_*end 41 command-line grep

我在一个目录中有一堆 .html 文件。我想查看每个文件并匹配一个模式(MD5)。所有这一切都很容易。问题是我需要知道匹配项是在哪个文件中找到的。

cat *.html | grep 75447A831E943724DD2DE9959E72EE31
Run Code Online (Sandbox Code Playgroud)

只返回找到匹配项的 HTML 页面内容,但它不会告诉我找到它的文件。如何让 grep 向我显示找到匹配项的文件名?

Cyr*_*rus 48

grep -H 75447A831E943724DD2DE9959E72EE31 *.html
Run Code Online (Sandbox Code Playgroud)
-H, --with-filename
              Print the file name for each match. This is
              the default when there is more than one file
              to search.
Run Code Online (Sandbox Code Playgroud)


the*_*der 26

我一直使用这个来查找包含字符串的文件,在目录中递归地(这意味着,遍历任何子子文件夹) grep -Ril "yoursearchtermhere"

  • R 是递归搜索(遵循符号链接)
  • i 是让它不区分大小写
  • l 只是列出文件的名称。

所以回答你的问题 grep -l '75447A831E943724DD2DE9959E72EE31' *.html可以,但你可以 grep -Ril '75447A831E943724DD2DE9959E72EE31'在任何子文件夹的任何文件中查找该字符串,不区分大小写


小智 7

你可以试试这个

grep -rl '75447A831E943724DD2DE9959E72EE31' * > found.txt
Run Code Online (Sandbox Code Playgroud)