how - 如何在Linux上找到包含特定文本的所有文件?
linux search file content (20)
我正在尝试找到一种方法来扫描整个Linux系统,查找包含特定文本字符串的所有文件。 只是为了澄清,我在文件中寻找文本,而不是文件名。
当我查找如何做到这一点时,我遇到了两次这个解决方案:
find / -type f -exec grep -H 'text-to-find-here' {} \;
但是,它不起作用。 它似乎显示系统中的每个文件。
这是否接近正确的方法呢? 如果没有,我该怎么办? 这种在文件中查找文本字符串的能力对于我正在做的一些编程项目非常有用。
如何在Linux上找到包含特定文本的所有文件? (......)
我两次遇到这个解决方案:
find / -type f -exec grep -H 'text-to-find-here' {} \;
如果在你的例子中使用find ,最好在命令末尾添加-s
( - --no-messages
)和命令末尾的2>/dev/null
,以避免grep
和find
发出的许多Permission denied消息:
grep -RIl "" .
find是在类Unix平台上搜索文件的标准工具 - 在查找特定文本时与grep结合使用。 顺便说一句, find命令通常与xargs结合使用。
为同一目的存在更快更容易的工具 - 见下文。 如果它们在您的平台上可用,请更好地尝试它们,当然:
更快更容易的替代品
ripgrep - 最快的搜索工具:
find / -type f -exec grep -sH 'text-to-find-here' {} \; 2>/dev/null
银色搜索者 :
rg 'text-to-find-here' / -l
ack :
ag 'text-to-find-here' / -l
注意:您也可以向这些命令添加2>/dev/null
,以隐藏许多错误消息。
警告 : 除非你真的无法避免,否则不要在'/' (根目录)中搜索以避免长时间和低效的搜索! 因此,在上面的示例中,您最好用子目录名替换' / ',例如“/ home”取决于您实际想要搜索的位置...
包含给定文本的文件名列表
首先,我相信您使用了-H
而不是-l
。 您也可以尝试在引号内添加文本,然后添加{} \
。
find / -type f -exec grep -l "text-to-find-here" {} \;
例
假设您正在目录中搜索包含特定文本“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$
区分大小写敏感度
即使您不使用“text”与“TEXT”之类的情况,也可以使用-i
开关忽略大小写。 您可以here阅读更多详细信息
希望这对你有所帮助。
Silver Searcher是一个了不起的工具,但ripgrep可能会更好。
它可以在Linux,Mac和Windows上运行,几个月前在Hacker News上编写(这有一个链接到Andrew Gallant的Blog,它有一个GitHub链接):
grep是你实现这一目标的好朋友。
grep -r <text_fo_find> <directory>
如果你不关心文本的情况下找到然后使用
grep -ir <text_to_find> <directory>
以下是可用于搜索文件的几个命令列表。
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
你可以使用ack 。 它就像grep的源代码。 您可以使用它扫描整个文件系统。
做就是了:
ack 'text-to-find-here'
在根目录中。
您还可以使用正则表达式 ,指定文件类型等。
UPDATE
我刚刚发现了The Silver Searcher ,它类似于ack但比它快3-5倍,甚至忽略了.gitignore
文件中的模式。
你可以用这个:
grep -inr "Text" folder/to/be/searched/
使用pwd
从您所在的任何目录中搜索,向下递归
grep -rnw `pwd` -e "pattern"
更新根据您使用的grep版本,您可以省略pwd
。 在较新的版本.
如果没有给出目录,似乎是grep的默认情况:
grep -rnw -e "pattern"
要么
grep -rnw "pattern"
会做同上面的事情!
即使我们没有找到字符串,也可以使用grep
。
简单地跑,
ack 'text-to-find-here' / -l
将打印出所有文本文件的路径,即仅包含可打印字符的文件。
如果你的grep
不支持递归搜索,你可以将find
与xargs
结合使用:
find / -type f | xargs grep 'text-to-find-here'
我觉得这比find -exec
的格式更容易记住。
这将输出文件名和匹配行的内容,例如
/home/rob/file:text-to-find-here
您可能想要添加到grep
可选标志:
-
-i
- 不区分大小写的搜索 -
-l
- 仅输出找到匹配项的文件名 -
-h
- 仅输出匹配的行(不是文件名)
尝试:
find / -type f -exec grep -H 'text-to-find-here' {} \;
这将搜索所有文件系统,因为/
是根文件夹。
对于主文件夹使用:
find ~/ -type f -exec grep -H 'text-to-find-here' {} \;
对于当前文件夹使用:
find ./ -type f -exec grep -H 'text-to-find-here' {} \;
希望这有助于......
稍微扩展grep
以在输出中提供更多信息,例如,获取文本所在文件中的行号可以按如下方式完成:
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"
选项的简短说明:
-
.
在find
指定当前目录。 -
-name
“*.*
”:表示所有文件(-name“*.pas
”-o -name“*.dfm
”):只有*.pas
OR*.dfm
文件,或者用-o
指定 -
-type f
指定您正在查找文件 -
-print0
和--null
在另一侧 (管道)是关键的,将文件名从find
发送到嵌入在xargs
的grep
,允许在文件名中传递带有空格的文件名,允许grep将路径和文件名视为一个字符串,而不是破坏它在每个空间。
我写了一个Python脚本 ,它做了类似的事情。 这就是人们应该如何使用这个脚本。
./sniff.py path pattern_to_search [file_pattern]
第一个参数path
是递归搜索的目录。 第二个参数pattern_to_search
是我们想要在文件中搜索的正则表达式。 我们使用Python re
库中定义的正则表达式格式。 在这个脚本中, .
也匹配换行。
第三个参数file_pattern
是可选的。 这是另一个适用于文件名的正则表达式。 仅考虑与此正则表达式匹配的那些文件。
例如,如果我想搜索扩展名py
包含Pool(
Python文件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
瞧,它会生成匹配文件的路径和找到匹配项的行号。 如果找到多个匹配项,则每个行号将附加到文件名。
我很着迷于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'
使用'-r'而不是'l'来查看文件名后面跟着找到模式的文本 !
grep -r 'pattern_to_find' /path/where/to/find
工作得很完美..
希望能帮助到你!
有一个ack
工具可以完全满足您的需求。
http://linux.die.net/man/1/ack
ack -i search_string folder_path/*
您可以忽略-i
用于区分大小写的搜索
有一个名为The Silversearcher的新工具
sudo apt install silversearcher-ag
它与Git和其他VCS密切合作。 所以你不会在.git或其他目录中获得任何东西。
你可以简单地使用
ag -ia "Search query"
它会为你完成任务!
要搜索字符串并使用搜索字符串输出该行:
for i in $(find /path/of/target/directory -type f); do grep -i "the string to look for" "$i"; done
例如:
for i in $(find /usr/share/applications -type f); \
do grep -i "web browser" "$i"; done
要显示包含搜索字符串的文件名:
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;
例如:
for i in $(find /usr/share/applications -type f); \
do if grep -i "web browser" "$i" > /dev/null; then echo "$i"; \
fi; done;
试试这个:
find . | xargs grep 'word' -sl
避免麻烦并安装ack-grep。 它消除了许多许可和报价问题。
apt-get install ack-grep
然后转到要搜索的目录并运行以下命令
cd /
ack-grep "find my keyword"
find /path -type f -exec grep -l "string" {} \;
评论解释
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