我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
当前回答
grep "text-to-find-here" file_name
或
grep "text-to-find-here" directory_path/*
如果你想搜索当前的目录:
grep "text-to-find-here" *
其他回答
试试这:
find . | xargs grep 'word' -sl
希望这就是帮助......
要在输出中提供更多信息,例如,在文件中获取字符号,文本可以如下:
find . -type f -name "*.*" -print0 | xargs --null grep --with-filename --line-number --no-messages --color --ignore-case "searthtext"
如果您有一个想法,文件类型是什么,您可以通过指定文件类型扩展来缩小您的搜索,在此情况下,.pas 或.dfm 文件:
find . -type f \( -name "*.pas" -o -name "*.dfm" \) -print0 | xargs --null grep --with-filename --line-number --no-messages --color --ignore-case "searchtext"
以下是选项的简短解释:
在搜索中,从当前目录中指定 - 名称“**” : 所有文件( - 名称“*.pas” -o - 名称“*.dfm” ) : 只有 *.pas 或 *.dfm 文件, 或 与 -o -type f 指定, 您正在寻找文件 -print0 和 -null 是关键的, 将文件名从搜索中转移到嵌入在 xargs 的文件名, 允许通过文件名。
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
下面是可以用于搜索文件的几个命令列表。
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
你可以使用:
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