如何在Linux上找到包含特定文本的所有文件?

Nat*_*han 4914 linux directory grep text find

我正在尝试找到一种方法来扫描整个Linux系统,查找包含特定文本字符串的所有文件.只是为了澄清,我在文件中寻找文本,而不是文件名.

当我查找如何做到这一点时,我遇到了两次这个解决方案:

find / -type f -exec grep -H 'text-to-find-here' {} \;
Run Code Online (Sandbox Code Playgroud)

但是,它不起作用.它似乎显示系统中的每个文件.

这是否接近正确的方法呢?如果没有,我该怎么办?这种在文件中查找文本字符串的能力对于我正在做的一些编程项目非常有用.

rak*_*ib_ 8841

请执行下列操作:

grep -rnw '/path/to/somewhere/' -e 'pattern'
Run Code Online (Sandbox Code Playgroud)
  • -r或者-R是递归的,
  • -n 是行号,和
  • -w 代表整个单词的匹配.
  • -l (小写L)可以添加到只给出匹配文件的文件名.

除了这些,--exclude,--include,--exclude-dir标志可用于高效搜索:

这对我来说非常有效,可以达到和你一样的目的.

有关更多选项检查man grep.

  • 值得注意的是:似乎`r`选项是懒惰的(遍历深度优先,而不是在第一个目录之后停止),而`R`是贪婪的(将正确遍历整个树). (77认同)
  • 使用--exclude.比如"grep -rnw --exclude =*.o'目录'-e"模式" (69认同)
  • @Eliran Malka`R`en`r`将正确遍历目录,但`R`将遵循符号链接. (61认同)
  • 注意(特别是对于新手):上述命令中的引号很重要. (30认同)
  • *grep -rnw"我正在寻找的字符串"*完成了我需要的工作.谢谢! (3认同)
  • 您还可以添加 --colour/--color 以突出显示您的搜索词。例如`grep -rnw --colour 。-e“终端”` (3认同)
  • -e 代表什么? (3认同)
  • 对我来说,添加“-I”选项来跳过所有二进制文件很有用。 (3认同)
  • 我该怎么做才能忽略二进制文件呢? (2认同)
  • 不要忘记通过在命令末尾添加“2> /dev/null”来告诉 Linux 不要向您显示您不需要的东西。这是唯一不会收到大量 Permission Denied 警告的方法,这些警告只会混淆您正在寻找的结果。 (2认同)
  • 它是否在目录中的隐藏文件/目录中搜索? (2认同)
  • 谁能解释为什么包含参数--include = \\ *。{c,h}中包含\?谢谢 (2认同)
  • 我发现`-i`选项对于"忽略大小写"也非常有用.也许把它放在你的选项列表中. (2认同)
  • 这是一个可靠的答案,但问题是它只能匹配整个单词,因为它使用了 `-w` 参数。我发现它不会匹配任意文本。 (2认同)

fed*_*qui 1399

你可以使用grep -ilR:

grep -Ril "text-to-find-here" /
Run Code Online (Sandbox Code Playgroud)
  • i 代表忽略大小写(在您的情况下是可选的).
  • R 代表递归.
  • l 代表"显示文件名,而不是结果本身".
  • / 代表从机器的根开始.

  • 根据我的经验,`-i`使它减慢很多,所以如果没有必要,不要使用它.在某个目录中测试然后进行推广.它应该在几分钟内完成.我认为正则表达式会让它变慢.但我的评论是基于假设,我建议你用前面的'time`来测试它. (80认同)
  • 您可以将目录`grep -Ril"替换为/ text-to-find-here"〜/ sites /`或使用.当前目录`grep -Ril"text-to-find-here".` (17认同)
  • 如果您不使用正则表达式进行搜索,则可以在大多数系统上使用fgrep代替grep. (10认同)
  • 是的@ markle976,实际上来自man grep:`fgrep与grep -F相同 - >解释PATTERN作为固定字符串列表`. (8认同)
  • 是的,`/*`代表那个.无论如何,我只是测试它,并注意到只有`/`工作. (4认同)
  • @Nathan 在所有这些时间之后(并且超过一百万次观看!)我注意到您可以添加参数`-I`(大写 i)来排除二进制文件。我认为这是这里的关键点,而不是 `exclude` 或 `include`。迟到总比不到好:D (3认同)
  • @Cleb 见 [grep,但只有某些文件扩展名](/sf/ask/876185621/) (2认同)

Ste*_*han 308

你可以使用ack.它就像grep的源代码.您可以使用它扫描整个文件系统.

做就是了:

ack 'text-to-find-here'
Run Code Online (Sandbox Code Playgroud)

在根目录中.

您还可以使用正则表达式,指定文件类型等.


UPDATE

我刚刚发现了The Silver Searcher,它类似于ack但比它快3-5倍,甚至忽略了.gitignore文件中的模式.

  • 非常有用,简单而快速.警告:"在Debian派生的发行版上,ack打包为"ack-grep",因为"ack"已经存在"(来自http://beyondgrep.com/install/).您最终可能会在这些Linux上运行一个汉字代码转换器...... (55认同)
  • ack或ack-grep有很好的亮点,但是当正确使用时性能要好得多,找到+ grep (11认同)
  • 请注意,[ripgrep](https://github.com/BurntSushi/ripgrep)比这里提到的任何其他内容都要快,包括The Silver Searcher和简单的'ol grep.有关证据,请参阅[此博客文章](http://blog.burntsushi.net/ripgrep/). (11认同)

lea*_*_19 177

您可以使用:

grep -r "string to be searched"  /path/to/dir
Run Code Online (Sandbox Code Playgroud)

r代表递归等都将在路径搜索指定的也是它的子目录.这将告诉您文件名以及打印出字符串出现的文件中的行.

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

find . -name '*.js' -exec grep -i 'string to search for' {} \; -print
Run Code Online (Sandbox Code Playgroud)

这将打印出现文本的文件中的行,但不会打印文件名.

除了这个命令,我们也可以写这个: grep -rn"要搜索的字符串"/ path /到/ directory /或/ file -r:递归搜索 n:将显示匹配的行号

  • 谢谢你的'发现'版本!能够按'*.js'或'*.txt'等过滤是非常重要的.没有人愿意花几个小时等待grep完成搜索上一次家庭度假的所有多GB视频,即使命令更容易输入. (3认同)

A R*_*A R 109

你可以用这个:

grep -inr "Text" folder/to/be/searched/
Run Code Online (Sandbox Code Playgroud)

  • 最简单,详细,递归和不区分大小写.竖起大拇指. (11认同)

sha*_*ora 79

当您在 Linux 上搜索特定文本时,此 grep 命令将为您提供精确的结果 -

grep -inRsH "Text to be searched" /path/to/dir (it can be '.')

  • i代表忽略大小写区别

  • R代表递归,它还包括符号链接。最好使用“R”而不是“r”

  • n代表“它将打印行号”。

  • s代表“抑制错误消息”

  • H代表“它将打印每个匹配的文件名”


lka*_*mal 67

包含给定文本的文件名列表

首先,我相信你用的是-H代替-l.您也可以尝试在引号后面添加文本{} \.

find / -type f -exec grep -l "text-to-find-here" {} \; 
Run Code Online (Sandbox Code Playgroud)

假设您正在搜索目录中包含特定文本"Apache License"的文件.它将显示与下面类似的结果(输出将根据您的目录内容而有所不同).

bash-4.1$ find . -type f -exec grep -l "Apache License" {} \; 
./net/java/jvnet-parent/5/jvnet-parent-5.pom
./commons-cli/commons-cli/1.3.1/commons-cli-1.3.1.pom
./io/swagger/swagger-project/1.5.10/swagger-project-1.5.10.pom
./io/netty/netty-transport/4.1.7.Final/netty-transport-4.1.7.Final.pom
./commons-codec/commons-codec/1.9/commons-codec-1.9.pom
./commons-io/commons-io/2.4/commons-io-2.4.pom
bash-4.1$ 
Run Code Online (Sandbox Code Playgroud)

区分大小写敏感度

即使您不使用"text"与"TEXT"之类的情况,也可以使用-i开关忽略大小写.你可以阅读更多细节在这里.

希望这对你有所帮助.

  • 这个命令的作用是什么:`find`将它找到的所有路径传递给命令`grep -l"text-to-find-here"<file found>"`.你可以对文件名添加限制,例如`find/-iname"*.txt"`只搜索名称以`.txt`结尾的文件 (2认同)
  • @Mene这是一个真正令人悲伤的状态,Auxiliary的评论比你的评论更多......即使他们的评论来自2014年,而你的评论是2017年,他们的评论是6,当它应该正好为0而你的只有一个(现在是两个)isn我想要相信的东西. (2认同)

ken*_*orb 61

grep(GNUBSD)

您可以使用grep工具递归搜索当前文件夹,例如:

grep -r "class foo" .
Run Code Online (Sandbox Code Playgroud)

注意:-r- 递归搜索子目录.

您还可以使用通配语法在特定文件中进行搜索,例如:

grep "class foo" **/*.c
Run Code Online (Sandbox Code Playgroud)

注意:通过使用globbing选项(**),它以递归方式扫描具有特定扩展名或模式的所有文件.要启用此语法,请运行:shopt -s globstar.您还可以使用**/*.*所有文件(隐藏和不扩展)或任何其他模式.

如果您的错误是您的参数太长,请考虑缩小搜索范围,或使用find语法,例如:

find . -name "*.php" -execdir grep -nH --color=auto foo {} ';'
Run Code Online (Sandbox Code Playgroud)

或者使用ripgrep.

ripgrep

如果你正在处理更大的项目或大文件,你应该使用ripgrep,比如:

rg "class foo" .
Run Code Online (Sandbox Code Playgroud)

查看GitHub项目页面上的文档,安装步骤或源代码.

它比其他工具一样快多了GNU/BSD grep,ucg,ag,sift,ack,pt或类似的,因为它是建立在之上锈病的正则表达式引擎,它使用有限自动机,SIMD和积极文字的优化,使搜索速度非常快.

它支持忽略.gitignore文件中指定的模式,因此可以同时针对多个glob模式匹配单个文件路径.


您可以使用常用参数,例如:

  • -i - 不敏感的搜索.
  • -I - 忽略二进制文件.
  • -w - 搜索整个单词(与部分单词匹配相反).
  • -n - 显示你的比赛线.
  • -C/ --context(例如-C5) - 增加上下文,以便查看周围的代码.
  • --color=auto - 标记匹配的文本.
  • -H - 显示找到文本的文件名.
  • -c - 显示匹配行的计数.可以结合使用-H.

  • 对于吸入整个文件系统, rg 将远比几乎任何其他工具都少痛苦。 (8认同)
  • 我还发现扩展的通配符很有用。但请记住,如果文件数量真的很大,您可能会收到“参数列表太长”错误。(简单的 globbing 也容易出现这种错误)。 (2认同)

Rob*_*arl 54

如果您grep不支持递归搜索,则可以结合find使用xargs:

find / -type f | xargs grep 'text-to-find-here'
Run Code Online (Sandbox Code Playgroud)

我发现这比格式更容易记住find -exec.

这将输出文件名和匹配行的内容,例如

/home/rob/file:text-to-find-here
Run Code Online (Sandbox Code Playgroud)

您可能要添加的可选标志grep:

  • -i - 不区分大小写的搜索
  • -l - 仅输出找到匹配项的文件名
  • -h - 只输出匹配的行(不是文件名)

  • 如果`find`找不到任何东西,这相当于没有文件名的'grep'text-to-find-here'.这将挂起并等待用户输入!添加`--no-run-if-empty`作为`xargs`的选项. (3认同)
  • 如果文件或目录名称包含空格(xargs解释为分隔符的字符),则find和xargs的组合不会按预期工作.使用`find ... -exec grep ... +`.如果你坚持将find与xargs一起使用,请使用`-print0`和`-0`. (3认同)

enf*_*net 42

grep -insr "pattern" *
Run Code Online (Sandbox Code Playgroud)
  • i:忽略PATTERN和输入文件中的大小写区别.
  • n:在输入文件中使用基于1的行号为每行输出添加前缀.
  • s:禁止有关不存在或不可读文件的错误消息.
  • r:递归地读取每个目录下的所有文件.

  • @ AmosM.Carpenter我喜欢这个答案的一件事是指出抑制论证,它可以帮助滤除与获得我们实际想要的结果无关的噪音.Grep在某些"文件"上打印诸如"功能未实现","无效参数","资源不可用"等错误. (6认同)
  • 你能解释一下你的答案如何改进其他答案,或者它们与它们有多大不同? (3认同)

Eyn*_*ave 37

grep -lrnw '/root/Desktop/ipozal' -e 'geolocation'
Run Code Online (Sandbox Code Playgroud)

例如:

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

  • 欢迎来到堆栈溢出!请考虑添加此代码块的描述或解释。 (5认同)

Blu*_*zee 34

如何在Linux上找到包含特定文本的所有文件?(......)

我两次遇到这个解决方案:

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


如果在您的示例中使用find,则更好地添加-s(--no-messages)grep以及2>/dev/null在命令的末尾,以避免由和发出的大量Permission denied消息:grepfind

find / -type f -exec grep -sH 'text-to-find-here' {} \; 2>/dev/null
Run Code Online (Sandbox Code Playgroud)

find是在类Unix平台上搜索文件的标准工具 - 在查找特定文本时与grep结合使用.在查找命令经常联合 xargs的,顺便说一句.

为同一目的存在更快更容易的工具 - 见下文.如果它们在您的平台上可用,请更好地尝试它们,当然:

更快更容易的替代品

RipGrep - 最快的搜索工具:

rg 'text-to-find-here' / -l
Run Code Online (Sandbox Code Playgroud)

银色搜索者:

ag 'text-to-find-here' / -l
Run Code Online (Sandbox Code Playgroud)

确认:

ack 'text-to-find-here' / -l
Run Code Online (Sandbox Code Playgroud)

注意:您也可以添加2>/dev/null这些命令,以隐藏许多错误消息.


警告:除非你真的无法避免它,否则不要在'/'(根目录)中搜索以避免长时间和低效的搜索!因此,在上面的示例中,您最好用子目录名替换' / ',例如"/ home"取决于您实际想要搜索的位置...


Nei*_*wal 34

有一个名为The Silversearcher的新工具

sudo apt install silversearcher-ag
Run Code Online (Sandbox Code Playgroud)

它与Git和其他VCS密切合作.所以你不会在.git或其他目录中获得任何东西.

你可以简单地使用

ag "Search query"
Run Code Online (Sandbox Code Playgroud)

它会为你完成任务!


小智 29

尝试:

find . -name "*.txt" | xargs grep -i "text_pattern"
Run Code Online (Sandbox Code Playgroud)

  • 这实际上是一个很好的例子,当不使用像这样的`xargs`时......考虑一下.```echo"文件bar.txt有条"> bar.txt; echo"file foo bar.txt has foo bar">"foo bar.txt"; 回声"你永远不应该看到这个foo"> foo; 找 .-name"*.txt"| xargs grep -i foo#./ foo:你永远不应该看到这个foo```.这里的`xargs`与WRONG文件匹配,与预期的文件不匹配.要么使用`find .. -print0 | xargs -0 ...`但这是一个无用的管道或更好的`find ... -exec grep ... {} +` (5认同)

mah*_*ich 29

用于pwd从您所在的任何目录中搜索,向下递归

grep -rnw `pwd` -e "pattern"
Run Code Online (Sandbox Code Playgroud)

更新 根据您使用的grep版本,您可以省略pwd..如果没有给出目录,那么在较新版本上似乎是grep的默认情况:

grep -rnw -e "pattern"

要么

grep -rnw "pattern"

会做同上面的事情!

  • 使用`pwd`根本不需要,因为它是默认值.`grep -rnw"pattern"`就够了. (3认同)

Dor*_*ian 22

如果您位于 Git 存储库中,则可以使用:

git grep something
Run Code Online (Sandbox Code Playgroud)


Ale*_*min 19

grep 即使我们不寻找字符串也可以使用.

只需跑步,

grep -RIl "" .
Run Code Online (Sandbox Code Playgroud)

将打印出所有文本文件的路径,即仅包含可打印字符的文件.

  • 我没有看到这比使用单纯的`ls`或`find`更好(对于递归) (2认同)

Atu*_*ind 17

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

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
Run Code Online (Sandbox Code Playgroud)

  • 这增加了现有的答案是什么? (5认同)

AAA*_*lub 16

Silver Searcher是一个了不起的工具,但ripgrep可能会更好.

它可以在Linux,Mac和Windows上运行,并且在几个月前写在Hacker News上(这有一个链接到Andrew Gallant的Blog,它有一个GitHub链接):

Ripgrep - 一种新的命令行搜索工具


muh*_*yab 16

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

grep -Ril -e 'texttoSearch' /etc/yum.repos.d
Run Code Online (Sandbox Code Playgroud)


Vin*_*shi 15

find /path -type f -exec grep -l "string" {} \;
Run Code Online (Sandbox Code Playgroud)

评论解释

find是一个命令,可以让您在给定路径的子目录中查找文件和其他对象,如目录和链接.如果未指定文件名应满足的掩码,则枚举所有目录对象.

-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
Run Code Online (Sandbox Code Playgroud)


小智 15

希望这有助于......

扩展grep一点以在输出中提供更多信息,例如,获取文本所在文件中的行号可以按如下方式完成:

find . -type f -name "*.*" -print0 | xargs --null grep --with-filename --line-number --no-messages --color --ignore-case "searthtext"
Run Code Online (Sandbox Code Playgroud)

如果你知道文件类型是什么,你可以通过指定要搜索的文件类型扩展来缩小搜索范围,在这种情况下是.pasOR .dfm文件:

find . -type f \( -name "*.pas" -o -name "*.dfm" \) -print0 | xargs --null grep --with-filename --line-number --no-messages --color --ignore-case "searchtext"
Run Code Online (Sandbox Code Playgroud)

选项的简短说明:

  1. .find当前目录的指定中.
  2. -name" *.*":对于所有文件(-name" *.pas" - o -name" *.dfm"):仅指定*.pasOR *.dfm文件或OR-o
  3. -type f 指定您正在查找文件
  4. -print0并且--null|(管道)的另一端是关键的,将文件名从嵌入传递findgrep嵌入式xargs,允许在文件名中传递带有空格的文件名,允许grep将路径和文件名视为一个字符串,而不是在每个空间分手.


小智 15

尝试:

find / -type f -exec grep -H 'text-to-find-here' {} \;
Run Code Online (Sandbox Code Playgroud)

这将搜索所有文件系统,因为/是根文件夹.

对于主文件夹使用:

find ~/ -type f -exec grep -H 'text-to-find-here' {} \;
Run Code Online (Sandbox Code Playgroud)

对于当前文件夹使用:

find ./ -type f -exec grep -H 'text-to-find-here' {} \;
Run Code Online (Sandbox Code Playgroud)


dan*_*ter 15

一个简单的find工作方便.在您的~/.bashrc文件中将其别名:

alias ffind find / -type f | xargs grep
Run Code Online (Sandbox Code Playgroud)

启动新终端并发出:

ffind 'text-to-find-here'
Run Code Online (Sandbox Code Playgroud)


Dil*_*war 14

我写了一个Python脚本,它做了类似的事情.这就是人们应该如何使用这个脚本.

./sniff.py path pattern_to_search [file_pattern]
Run Code Online (Sandbox Code Playgroud)

第一个参数path是我们将递归搜索的目录.第二个参数pattern_to_search是我们想要在文件中搜索的正则表达式.我们使用Python re库中定义的正则表达式格式.在此脚本中,.还匹配换行符.

第三个参数file_pattern是可选的.这是另一个适用于文件名的正则表达式.仅考虑与此正则表达式匹配的那些文件.

例如,如果我想搜索py包含Pool(后跟单词的扩展名的Python文件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 
Run Code Online (Sandbox Code Playgroud)

瞧,它会生成匹配文件的路径和找到匹配项的行号.如果找到多个匹配项,则每个行号将附加到文件名.


Dr_*_*ope 13

使用:

grep -c Your_Pattern *
Run Code Online (Sandbox Code Playgroud)

这将报告当前目录中每个文件中有多少个模式副本.


小智 13

grep是你实现这一目标的好朋友.

grep -r <text_fo_find> <directory>
Run Code Online (Sandbox Code Playgroud)

如果你不关心文本的情况下找到然后使用

grep -ir <text_to_find> <directory>
Run Code Online (Sandbox Code Playgroud)


小智 12

要搜索字符串并使用搜索字符串输出该行:

for i in $(find /path/of/target/directory -type f); do grep -i "the string to look for" "$i"; done
Run Code Online (Sandbox Code Playgroud)

例如:

for i in $(find /usr/share/applications -type f); \
do grep -i "web browser" "$i"; done
Run Code Online (Sandbox Code Playgroud)

要显示包含搜索字符串的文件名:

for i in $(find /path/of/target/directory -type f); do if grep -i "the string to look for" "$i" > /dev/null; then echo "$i"; fi; done;
Run Code Online (Sandbox Code Playgroud)

例如:

for i in $(find /usr/share/applications -type f); \
do if grep -i "web browser" "$i" > /dev/null; then echo "$i"; \
fi; done;
Run Code Online (Sandbox Code Playgroud)

  • 与使用“find … -exec grep 'str' {} \;”相比,我只看到了缺点(如果您必须使用“find”)。 (2认同)
  • 如果“find”找到的任何文件包含空格,这将严重破坏..您可能最终“grepping”错误的文件和/或完全丢失正确的文件。如果您需要使用“find”..,只需使用“find ... -exec grep ...”,但在这种情况下“grep -r ...”就足够了。 (2认同)
  • 对 find 的结果使用循环然后 grep 有什么意义?这变得不必要的复杂。 (2认同)

Pal*_*Pal 12

有一种ack工具可以完全满足您的需求.

http://linux.die.net/man/1/ack

ack -i search_string folder_path/*
Run Code Online (Sandbox Code Playgroud)

您可以忽略-i区分大小写的搜索

  • 这增加了现有的答案是什么?三年多前就提出过这个建议. (2认同)
  • @fedorqui 1)没有管道!2)使用正则表达式 3)获取行号、带有相对路径的文件名、突出显示的文本等。对于搜索后的编辑很有用,例如“vim +lineno path/file.cpp”将让您直接到达感兴趣的行号。请参阅命令“ack include\|hpp”的输出,该命令在我的搜索文件夹和子文件夹下搜索“include”或“hpp”关键字。我希望这一点很清楚。这是示例输出(无法用简单文本显示关键字突出显示) process/child.hpp 11:boost/process/child.hpp process/all.hpp 21:#include &lt;boost/process/execute.hpp&gt; (2认同)

Pet*_*ica 12

所有以前的答案都建议grep并找到.但还有另一种方法:使用午夜指挥官

它是一个免费的实用程序(30岁,经过时间证明),它是视觉上没有GUI.有大量功能,查找文件只是其中之一.


nit*_*708 12

我很着迷于grep用'rl'做出的简单方法

grep -rl 'pattern_to_find' /path/where/to/find

-r to find recursively file / directory inside directories..
-l to list files matching the 'pattern'
Run Code Online (Sandbox Code Playgroud)

使用'-r'而不是'l'来查看文件名后面跟着找到模式文本!

grep -r 'pattern_to_find' /path/where/to/find
Run Code Online (Sandbox Code Playgroud)

工作得很完美..

希望能帮助到你!


Bre*_*dSP 12

如果您严格要使用find那么:

find + grep

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

1. find使用搜索文件,然后执行grep所有文件.

可以在一个命令中组合如下:

find

  • 使用-name Pattern,如果你想grep只有某些文件:

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

这可以让您find找到文件的强大功能.您可以使用它并使用不同的选项find来改进或缩小文件搜索范围.


Tay*_*ain 11

试试这个:

find . | xargs grep 'word' -sl
Run Code Online (Sandbox Code Playgroud)

  • 这比grep解决方案要慢得多 (4认同)

Mit*_*tel 11

您可以使用以下命令,因为您不想要文件名但想要从所有文件中搜索.这是我正在捕获"TEXT"表单所有日志文件,确保不打印文件名

grep -e TEXT *.log | cut -d' ' --complement -s -f1
Run Code Online (Sandbox Code Playgroud)

与其他选项相比,使用-e选项的grep非常快,因为它适用于PATTERN匹配


Pra*_*ami 11

以下命令适用于此方法:

find ./ -name "file_pattern_name"  -exec grep -r "pattern" {} \;
Run Code Online (Sandbox Code Playgroud)

  • 使用`find`然后`grep -r`是什么意思?它们也是相同的,所以这是多余的. (2认同)
  • 不过,这没有任何意义,你可以用`find`过滤. (2认同)

Kar*_*eem 11

避免麻烦并安装ack-grep.它消除了许多许可和报价问题.

apt-get install ack-grep
Run Code Online (Sandbox Code Playgroud)

然后转到要搜索的目录并运行以下命令

cd /
ack-grep "find my keyword"
Run Code Online (Sandbox Code Playgroud)


Leo*_*Leo 10

我的用例是找到我以前编写的以特定方式编写 jsonlines 的 Python 代码。我知道这jsonl将是函数名称的一部分并且to_json会出现在函数体内,但除此之外就没有什么了。

尽管有 50 个答案,但在同一文件中找到多个字符串(无论是否在同一行)尚未得到解答。

grep-q中的 表示安静。不打印任何内容,仅设置返回值。因此-print最后。-exec仅当前一个成功时,每个才会运行。因此,如果您有很多文件,那么考虑消除您不感兴趣的文件的模式是值得的。

find . -type f -name "*.py" \
  -exec grep -q -e 'to_json' {} \; \
  -exec grep -q -e 'def\s.*jsonl' {} \; \
  -print
Run Code Online (Sandbox Code Playgroud)


bal*_*ash 9

grep -Erni + "text you wanna search"
Run Code Online (Sandbox Code Playgroud)

该命令将在当前目录的所有文件和目录中以递归方式搜索并打印结果.

注意:如果你的grep输出没有着色,你可以在shell src文件中使用grep ='grep --color = always'别名来更改它


dki*_*zer 8

如果您有一组始终要检查的文件,则可以为其路径添加别名,例如:

alias fd='find . -type f -regex ".*\.\(inc\|info\|module\|php\|test\|install\|uninstall\)"'
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样过滤列表:

grep -U -l $'\015' $(fd)
Run Code Online (Sandbox Code Playgroud)

其中将列表fd筛选出包含CR模式的文件.

我发现别名我感兴趣的文件可以帮助我创建更简单的脚本,然后总是试图记住如何获取所有这些文件.递归的东西也可以工作,但迟早你将不得不面对清除特定的文件类型.这就是为什么我只是找到我感兴趣的所有文件类型的原因.


VIP*_*MAR 8

试试这个:

find / -type f -name "*" -exec grep -il "String_to_search" {} \;
Run Code Online (Sandbox Code Playgroud)

要么

for i in /*;do grep -Ril "String_to_search" $i;done 2> /dev/null
Run Code Online (Sandbox Code Playgroud)


小智 8

您可以使用以下命令从文件中查找特定文本:

cat file | grep 'abc' | cut -d':' -f2
Run Code Online (Sandbox Code Playgroud)

  • 无论如何,这是对 cat 的无用使用... http://porkmail.org/era/unix/award.html (2认同)

Gee*_*ode 8

正如彼得在上一篇回答中提到的那样,之前的所有答案都表明了grep并找到

但是有更复杂的方式使用Gnome Commander具有完美的GUI和自2001年以来的大量选项,找到文件只是其中之一.它是一种免费的实用工具,经过时间证明.


Mik*_*inn 8

findxarg■当存在很多潜在的去筛选匹配是优选的.它的运行速度比其他选项慢,但它始终有效.正如一些人所发现的xargs那样,默认情况下不处理带有嵌入空格的文件.您可以通过指定-d选项来克服此问题.

这是@ RobEarl的答案,增强了所以它处理带空格的文件:

find / -type f | xargs -d '\n' grep 'text-to-find-here'
Run Code Online (Sandbox Code Playgroud)

这是@ venkat的答案,同样增强了:

find . -name "*.txt" | xargs -d '\n' grep -i "text_pattern"
Run Code Online (Sandbox Code Playgroud)

这是@Gert van Biljon的答案,同样增强了:

find . -type f -name "*.*" -print0 | xargs -d '\n' --null grep --with-filename --line-number --no-messages --color --ignore-case "searthtext"
Run Code Online (Sandbox Code Playgroud)

这是@ LetalProgrammer的答案,同样增强了:

alias ffind find / -type f | xargs -d '\n' grep
Run Code Online (Sandbox Code Playgroud)

这是@Tayab Hussain的答案,同样增强了:

find . | xargs -d '\n' grep 'word' -sl
Run Code Online (Sandbox Code Playgroud)


小智 8

试试这个命令。这将为您提供包含您输入的模式的文件。

sudo grep -inr "your-pattern" /
Run Code Online (Sandbox Code Playgroud)

这里: i - 忽略大小写区别,以便仅大小写不同的字符相互匹配。

n - 确保实际行内容的第一个字符位于制表位上,以便制表符的对齐方式看起来正常。

r - 递归地读取每个目录下的所有文件,仅当它们位于命令行上时才遵循符号链接。请注意,如果未给出文件操作数,grep 将搜索工作目录。


Sir*_*dda 7

试试这个

find . -type f -name some_file_name.xml -exec grep -H PUT_YOUR_STRING_HERE {} \;
Run Code Online (Sandbox Code Playgroud)

  • @SergeyDenisov是什么给出的?这绝对是一个答案.(它是否有效是另一回事.) (4认同)
  • 这并没有提供问题的答案。要批评或要求作者澄清,请在其帖子下方发表评论。- [来自评论](/review/low-quality-posts/10250356) (2认同)
  • @jpaugh 那你应该详细解释一下。 (2认同)
  • @谢尔盖·丹尼索夫。它给出了可能产生正确结果的建议行动方案。或者,即使没有,它也可能对其他人有帮助。这就是我所说的“这是一个答案”。如果您想知道它是如何工作的,请询问海报。 (2认同)
  • @jpaugh我确定一行命令/代码不足以完整答案.您可以撰写评论,提供建议的行动方案,但答案应包括解释.这就是为什么这个答案被标记为"低质量帖子"(不是我). (2认同)

小智 7

我试图找到一种方法来扫描我整个 Linux 系统中包含特定文本字符串的所有文件。...这接近正确的方法吗?如果没有,我该怎么办?... 这种在文件中查找文本字符串的能力对于我正在做的一些编程项目非常有用。

虽然您永远不应该用不同的程序替换(或别名)系统命令,但由于脚本或其他实用程序的神秘损坏风险,如果您手动或从您自己的脚本或程序运行文本搜索,您应该考虑最快的合适的程序在多次搜索大量文件时使用。在对您描述的用例进行几次使用后,可以恢复花费十分钟到半小时的时间来安装和熟悉更好的实用程序。

提供“ ack、ag、git-grep、GNU grep 和 ripgrep 的功能比较”的网页可以帮助您确定哪个程序提供了您需要的功能。

  • Andrew Gallant 的博客声称:“ ripgrep 比 {grep, ag, git grep, ucg, pt, sift} 更快”(其他一些人也有这样的说法,这就是为什么进行功能比较很有帮助的原因)。特别感兴趣的是他关于正则表达式实现和陷阱的部分。

    以下命令搜索所有文件,包括隐藏文件和可执行文件:

    $ rg -uuu foobar

  • Silver Searcher (ag) 声称它比 Ack 快 5-10 倍。在其他一些答案中建议使用该程序。GitHub 似乎不像 ripgrep 那样新,并且有明显更多的提交和分支,而发布的版本更少,很难根据这些统计数据得出绝对的主张。在短版:ripgrep速度较快,但有一个很小的学习曲线,无法获得通过差异抓获。

  • 那么接下来会是什么,你猜对了,白金搜索者。声称是:它搜索代码比 ack 快 3-5 倍,但它的速度等于银搜索器。它是用 GoLang 编写的,可以搜索 UTF-8、EUC-JP 和 Shift_JIS 文件;如果那是更大的兴趣。GitHub 既不是特别新,也不是特别活跃。GoLang 本身有一个快速而强大的正则表达式,但如果它有更好的用户兴趣,最好推荐白金搜索器。

对于速度和强大的索引查询语言(例如ElasticSearchSolr)的组合,可以是一项有回报的长期投资,但如果您想要快速简单地替代 grep,则不是。OTOH 都有一个可以从您编写的任何程序中调用的 API,为您的程序添加强大的搜索功能。

虽然可以生成外部程序、执行搜索、拦截其输出并对其进行处理,但调用 API 是提高功率和性能的方法。

This question was protected Aug 6 '15 at 19:34 with this caution:
  我们正在寻找能够提供一些解释和背景的长篇答案。不要只给出一行答案;解释为什么你的答案是正确的,最好是引用。

虽然一些答案提出了完成搜索的替代方法,但除了“免费”、“更快”、“更复杂”、“大量功能”等之外,它们并没有解释为什么。不要试图出售它,只需告诉我们“为什么你的答案是正确的”。我试图教如何选择最适合用户的东西,以及为什么. 这就是为什么我提供另一个答案的原因,因为已经有这么多答案了。否则我会同意已经有很多答案了;我希望我带来了很多新东西。


huu*_*ang 7

grep "text-to-find-here" file_name
Run Code Online (Sandbox Code Playgroud)

或者

grep "text-to-find-here" directory_path/*
Run Code Online (Sandbox Code Playgroud)

如果要搜索当前目录:

grep "text-to-find-here" *
Run Code Online (Sandbox Code Playgroud)

  • 如果您要递归地查找子目录,请确保在“grep”之后添加“-r”开关。 (7认同)

int*_*ika 6

GUI搜索备选方案-桌面使用:

-由于问题并非精确地要求命令

Searchmonkey:高级文件搜索工具,而无需使用正则表达式对系统进行索引。图形等效于find / grep。适用于Linux(Gnome / KDE / Java)和Windows(Java)-开源GPL v3

特征:

  • 高级正则表达式
  • 结果显示在上下文中
  • 搜索包含文字
  • 面板显示包含文本的行
  • 2018年新更新
  • 等等

下载-链接:

屏幕截图:

在此处输入图片说明


Gus*_*ulo 6

另请参阅Platinium Searcher,它类似于The Silver Searcher,它是用Go编写的.

例:

pt -e 'text to search'
Run Code Online (Sandbox Code Playgroud)

  • 欢迎提供解决方案的链接,但请确保您的答案在没有它的情况下也是有用的:[在链接周围添加上下文](//meta.stackexchange.com/a/8259),这样您的其他用户就会知道它是什么,并且为什么它在那里,然后引用您链接到的页面中最相关的部分,以防目标页面不可用。[仅是链接的答案可能会被删除。](//stackoverflow.com/help/deleted-answers) (2认同)

小智 5

你的命令是正确的。你只需要-l在 grep 中添加:

find / -type f -exec grep -l 'text-to-find-here' {} \;
Run Code Online (Sandbox Code Playgroud)


Lev*_*von 5

您可以使用ripgrep它将默认尊重项目的.gitignore文件

ripgrep

抑制Permission denied错误

rg -i rustacean 2> /dev/null
Run Code Online (Sandbox Code Playgroud)

这会将stderr(标准错误输出)重定向到/dev/null


小智 5

请根据需求自定义以下命令,并从文件中递归查找任意字符串。

grep -i hack $(find /etc/ -type f)
Run Code Online (Sandbox Code Playgroud)