我如何在其文件内容中找到包含一个特定的文本序列的所有文件?

下一个不起作用. 它似乎显示系统中的每个单一文件。

find / -type f -exec grep -H 'text-to-find-here' {} \;

当前回答

这个捕捉命令会给你一个准确的结果,当你正在寻找特定的文本在Linux -

grep -inRsH “文本要被搜索” /path/to/dir(它可以是“。

i stands for ignore case distinctions R stands for recursive and it also includes symlinks. It is better to use 'R' instead of 'r' n stands for "it will print line number." s stands for "suppress error messages" H stands for "it will print the file name for each match"

其他回答

我的使用案例是找到我写的Python代码,我写的JSON线的某种方式,我知道JSONL将是函数名称的一部分,到_JSON将出现在身体中,但没有更多的东西。

尽管有 50 个答案,但在同一文件中找到多个字符串(无论是在同一行中还是不在同一行中)并没有得到答案。

格雷普中的 -q 是安静的. 没有什么是打印,只有返回值是设置的. 因此,在结尾的 - 打印. 每个 -exec 只运行,如果前一个成功。

find . -type f -name "*.py" \
  -exec grep -q -e 'to_json' {} \; \
  -exec grep -q -e 'def\s.*jsonl' {} \; \
  -print

我尝试了下面的捕捉命令,它有助于在 /etc/yum.repos.d 我的存储库中搜索内容。

grep -Ril -e 'texttoSearch' /etc/yum.repos.d

我写了一个Python脚本,它做了一些类似的事情,这就是如何使用这个脚本。

./sniff.py path pattern_to_search [file_pattern]

第一個論點,路径,是我們將重新搜尋的目錄. 第二個論點, pattern_to_search,是一個常見的表現,我們想要在檔案中搜尋。

第三个论点, file_pattern,是可选的. 这是另一个常见的表达式,在一个文件名上工作. 只有与这个常见的表达式相匹配的文件将被考虑。

例如,如果我想搜索Python文件的扩展Py包含Pool(跟随“Adaptor”这个词,我做以下,

./sniff.py . "Pool(.*?Adaptor"  .*py
./Demos/snippets/cubeMeshSigNeur.py:146 
./Demos/snippets/testSigNeur.py:259 
./python/moose/multiscale/core/mumbl.py:206 
./Demos/snippets/multiComptSigNeur.py:268 

而 voila,它产生了匹配文件的路径和线号,在那里比赛被发现,如果超过一个比赛被发现,那么每个线号将添加到文件名。

这个捕捉命令会给你一个准确的结果,当你正在寻找特定的文本在Linux -

grep -inRsH “文本要被搜索” /path/to/dir(它可以是“。

i stands for ignore case distinctions R stands for recursive and it also includes symlinks. It is better to use 'R' instead of 'r' n stands for "it will print line number." s stands for "suppress error messages" H stands for "it will print the file name for each match"

试试:

find . -name "*.txt" | xargs grep -i "text_pattern"