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

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

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

当前回答

你的命令是正确的. 你只需要添加 -l 来抓住:

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

其他回答

我写了一个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,它产生了匹配文件的路径和线号,在那里比赛被发现,如果超过一个比赛被发现,那么每个线号将添加到文件名。

重复和案例不敏感的捕捉与线号:

grep -inr "Text" folder/to/be/searched/
grep -lrnw '/root/Desktop/ipozal' -e 'geolocation'

例如:

我的文件夹名称是“Ipozal”它放在“/root/Desktop”我想在所有文件中找到此文本“地理位置”

find /path -type f -exec grep -l "string" {} \;

评论的解释

查找是一个命令,允许您在特定路径的子目录中找到文件和其他对象,如目录和链接,如果您不指定该文件名称应满足的面具,则列出所有目录对象。

-type f specifies that it should proceed only files, not directories etc.
-exec grep specifies that for every found file, it should run grep command, passing its filename as an argument to it, by replacing {} with the filename

试试这:

find . | xargs grep 'word' -sl