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

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

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

当前回答

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

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

其他回答

你可以使用:

grep -r "string to be searched"  /path/to/dir

r 是可重复的,因此将在指定的路径和其子指南中进行搜索,这将告诉您文件名以及在文件中印刷线的字符串。

或类似于您正在尝试的命令(例如: )在所有JavaScript文件中搜索( (*.js):

find . -name '*.js' -exec grep -i 'string to search for' {} \; -print

这将打印文本显示的文件中的行,但它不会打印文件名。

除了此命令之外,我们还可以写下: grep -rn “String to search” /path/to/directory/or/file -r: recursive search n: line number will be shown for matches

下面是可以用于搜索文件的几个命令列表。

grep "text string to search” directory-path

grep [option] "text string to search” directory-path

grep -r "text string to search” directory-path

grep -r -H "text string to search” directory-path

egrep -R "word-1|word-2” directory-path

egrep -w -R "word-1|word-2” directory-path

如果您严格想要使用查找,则使用查找 + 查找:

找到 /path/to/somewhere/ -type f -exec grep -nw 'textPattern' {} \;

步骤:

使用查找搜索文件,执行捕获所有。

这给了你找到文件的权力。

使用 -name 模式,如果您只想捕捉某些文件:

找到 /path/to/somewhere/ -type f -name \*.cpp -exec grep -nw 'textPattern' {} \;

您可以使用不同的搜索选项来改善您的文件搜索。

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

正如彼得在以前的回答中提到的那样,所有以前的回答都建议抓住并找到。

但有一个更复杂的方式使用Gnome Commander与完美的GUI和数以百万计的选项自2001年以来,并找到文件只是其中之一。