提问人:Nathan 提问时间:6/6/2013 最后编辑:Peter MortensenNathan 更新时间:11/11/2023 访问量:10636999
如何在 Linux 上找到包含特定文本(字符串)的所有文件?
How can I find all files containing a specific text (string) on Linux?
问:
如何找到在其文件内容中包含特定文本字符串的所有文件?
以下操作不起作用。它似乎显示系统中的每个文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
答:
用:grep -ilR
grep -Ril "text-to-find-here" /
i
代表忽略大小写(在您的案例中是可选的)。R
代表递归。l
代表“显示文件名,而不是结果本身”。/
代表从机器的根部开始。
评论
-i
time
grep -Ril "text-to-find-here" ~/sites/
grep -Ril "text-to-find-here" .
grep -ril "name" /
执行以下操作:
grep -Rnw '/path/to/somewhere/' -e 'pattern'
-r
或者是递归的 ;用于完全搜索-R
-R
-n
是行号,并且-w
代表匹配整个单词。-l
可以添加(小写 L)以仅提供匹配文件的文件名。-e
是搜索期间使用的模式
除了这些, , , 标志可用于有效的搜索:--exclude
--include
--exclude-dir
这将仅搜索扩展名为 .c 或 .h 的文件:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
这将排除搜索所有以 .o 扩展名结尾的文件:
grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern"
对于目录,可以使用该参数排除一个或多个目录。例如,这将排除 dirs ,并且所有目录都匹配:
--exclude-dir
dir1/
dir2/
*.dst/
grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/search/' -e "pattern"
这对我来说非常有效,可以达到与您几乎相同的目的。
有关更多选项,请参阅 man grep
。
评论
r
R
-I
-rnw
区分大小写,且仅是整个单词。对于更广泛的搜索,更改为为生成整个或部分单词的匹配项,而不考虑字母大小写。-rnw
-rni
您可以使用 ack。它就像源代码的 grep。您可以使用它扫描整个文件系统。
只要做:
ack 'text-to-find-here'
在根目录中。
您还可以使用正则表达式、指定文件类型等。
更新
我刚刚发现了 The Silver Searcher,它就像 ack 一样,但比它快 3-5 倍,甚至忽略文件中的模式。.gitignore
评论
ag 'what I want' ./
递归和不区分大小写的 grep 带行号:
grep -inr "Text" folder/to/be/searched/
评论
包含给定文本的文件名列表
首先,我相信你已经用了 .您也可以尝试在引号内添加文本,后跟 .-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
评论
find
grep -l "text-to-find-here" <file found>"
find / -iname "*.txt"
.txt
-iname
不区分大小写,这意味着它还可以找到.TXT文件,例如,以及 TxT 和 TXt 等。
我写了一个 Python 脚本来做类似的事情。这就是应该如何使用这个脚本。
./sniff.py path pattern_to_search [file_pattern]
第一个参数 是我们将在其中递归搜索的目录。第二个参数 是我们想要在文件中搜索的正则表达式。我们使用 Python 库中定义的正则表达式格式。在此脚本中,还匹配换行符。path
pattern_to_search
re
.
第三个参数 是可选的。这是另一个适用于文件名的正则表达式。只有与此正则表达式匹配的文件才会被考虑。file_pattern
例如,如果我想搜索扩展名后跟 word 的 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
瞧,它会生成匹配文件的路径和找到匹配项的行号。如果找到多个匹配项,则每个行号都将附加到文件名中。
要搜索字符串并仅输出带有搜索字符串的那一行,请执行以下操作:
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 … -exec grep 'str' {} \;
find
find
grepping
find ... -exec grep ...
find
grep -r ...
以下是可用于搜索文件的几个命令列表。
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
评论
egrep
grep -E
--extended-regexp
您可以使用:
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
即使我们不是在寻找字符串,也可以使用。
简单的运行,
grep -RIl "" .
将打印出所有文本文件的路径,即仅包含可打印字符的文件。
评论
ls
find
如果您不支持递归搜索,您可以结合: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
- 只输出匹配的行(不是文件名)
评论
grep 'text-to-find-here'
find
--no-run-if-empty
xargs
find … -exec grep … +
-print0
-0
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
尝试:
find . -name "*.txt" | xargs grep -i "text_pattern"
评论
xargs
echo "file bar.txt has bar" > bar.txt; echo "file foo bar.txt has foo bar" > "foo bar.txt"; echo "You should never see this foo" > foo; find . -name "*.txt" | xargs grep -i foo # ./foo:You should never see this foo
xargs
find .. -print0 | xargs -0 ...
find ... -exec grep ... {} +
扩展 a 位以在输出中提供更多信息,例如,获取文本所在的文件中的行号,可以按如下方式完成:grep
find . -type f -name "*.*" -print0 | xargs --null grep --with-filename --line-number --no-messages --color --ignore-case "searthtext"
如果您知道文件类型是什么,您可以通过指定要搜索的文件类型扩展名来缩小搜索范围,在本例中为 OR 文件:.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 “” -o -name “” ) : 仅 OR 文件,OR 指定*.pas
*.dfm
*.pas
*.dfm
-o
-type f
指定要查找的文件-print0
在(管道)的另一边是关键的,将文件名从 传递到 嵌入在 中,允许在文件名中传递带有空格的文件名,允许 grep 将路径和文件名视为一个字符串,而不是在每个空格上将其分解。--null
|
find
grep
xargs
评论
-name '*.*'
不是你说的;它不会接收名为“file”的文件,因为该模式不等同于该文件(没有 .ext); 然而,会(好吧,文件放在一边)。但还有另一件事:如果你想要所有文件,为什么首先要指定一个文件名呢?没有其他评论 - 除了很高兴知道仍然有人不使用 MS 术语“文件夹”(真的,在说得够多之后,我不会添加,但我想指出你对文件名所做的稍微不正确的陈述 - 以及“全部”情况下的冗余/无用)。*
print0
也应该解释。是文件名中的空格吗?
尝试:
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
(GNU 或 BSD)
您可以使用工具递归搜索当前文件夹,例如:grep
grep -r "class foo" .
注意: -r -
递归搜索子目录。
还可以使用通配语法在特定文件中进行搜索,例如:
grep "class foo" **/*.c
注意:通过使用通配选项 (**
),它会以递归方式扫描具有特定扩展名或模式的所有文件。若要启用此语法,请运行:shopt -s globstar
。您也可以将 **/*.*
用于所有文件(不包括隐藏和不带扩展名)或任何其他模式。
如果您发现参数太长的错误,请考虑缩小搜索范围,或改用语法,例如:find
find . -name "*.php" -execdir grep -nH --color=auto foo {} ';'
或者,使用 ripgrep
。
ripgrep
如果您正在处理较大的项目或大文件,则应改用,例如:ripgrep
rg "class foo" .
查看 GitHub 项目页面上的文档、安装步骤或源代码。
它比任何其他工具(如 GNU/BSD grep
、ucg
、ag
、sift
、ack
、pt
或类似工具)都要快得多,因为它是建立在 Rust 的正则表达式引擎之上的,该引擎使用有限自动机、SIMD 和激进的文字优化来使搜索非常快。
它支持忽略文件中指定的模式,因此单个文件路径可以同时与多个 glob 模式进行匹配。.gitignore
您可以使用常用参数,例如:
-i
- 不敏感的搜索。-I
- 忽略二进制文件。-w
- 搜索整个单词(与部分单词匹配相反)。-n
- 显示您的匹配线。-C
/--context
(例如) - 增加上下文,以便您看到周围的代码。-C5
--color=auto
- 标记匹配的文本。-H
- 显示找到文本的文件名。-c
- 显示匹配行数。可以与 . 结合使用-H
评论
如何在 Linux 上找到包含特定文本的所有文件? (...)
我两次遇到这个解决方案:
find / -type f -exec grep -H 'text-to-find-here' {} \;
如果像在示例中使用 find 一样,最好在命令末尾添加 () 和 ,以避免 和 发出大量 Permission denied 消息:-s
--no-messages
grep
2>/dev/null
grep
find
find / -type f -exec grep -sH 'text-to-find-here' {} \; 2>/dev/null
find 是用于在类 Unix 平台上搜索文件的标准工具 - 在查找特定文本时与 grep 结合使用。顺便说一句,find 命令通常与 xargs 结合使用。
更快、更简单的工具也用于相同的目的 - 见下文。当然,最好尝试一下,前提是它们在您的平台上可用:
更快、更简单的替代方案
RipGrep - 最快的搜索工具:
rg 'text-to-find-here' / -l
ag 'text-to-find-here' / -l
确认:
ack 'text-to-find-here' / -l
注意:您也可以添加到这些命令中,以隐藏许多错误消息。2>/dev/null
警告:除非你真的无法避免它,否则不要从“/”(根目录)搜索,以避免长时间和低效的搜索! 因此,在上面的示例中,您最好将“/”替换为子目录名称,例如“/home”,具体取决于您实际要搜索的位置......
grep -insr "pattern" *
i
:忽略 PATTERN 和输入文件中的大小写区别。n
:在每行输出前加上其输入文件中从 1 开始的行号。s
:禁止显示有关不存在或不可读文件的错误消息。r
:以递归方式读取每个目录下的所有文件。
评论
用于从您所在的任何目录进行搜索,向下递归pwd
grep -rnw `pwd` -e "pattern"
根据您使用的 grep 版本,您可以省略 .在较新的版本中,如果没有给出目录,这似乎是 grep 的默认情况。pwd
.
因此:
grep -rnw -e "pattern"
或
grep -rnw "pattern"
会做和上面一样的事情!
评论
pwd
grep -rnw "pattern"
grep -rnw
grep -rnw '/path/to/somewhere/' -e "pattern"
有一个 ack 工具可以完全满足您的需求:
ack -i search_string folder_path/*
对于区分大小写的搜索,您可以忽略。-i
评论
有一个名为 The Silversearcher 的新实用程序
sudo apt install silversearcher-ag
它与 Git 和其他 VCS 密切合作。因此,您不会在 .git 或其他目录中获得任何内容。
您可以简单地使用
ag "Search query"
它会为您完成任务!
评论
Silver Searcher 是一个了不起的工具,但 ripgrep 可能更好。
它适用于 Linux、Mac 和 Windows,几个月前在 Hacker News 上写了这篇文章(这有一个指向 Andrew Gallant 博客的链接,该博客有一个 GitHub 链接):
一个简单的可以派上用场。在文件中别名:find
~/.bashrc
alias ffind find / -type f | xargs grep
启动新终端并发出:
ffind 'text-to-find-here'
我着迷于grep使用'rl'的简单性:
grep -rl 'pattern_to_find' /path/where/to/find
-r to recursively find a file / directory inside directories..
-l to list files matching the 'pattern'
使用不带“l”的“-r”查看文件名,后跟找到模式的文本!
grep -r 'pattern_to_find' /path/where/to/find
它工作得很完美......
评论
.txt
Grep 是您实现这一目标的好朋友。
grep -r <text_fo_find> <directory>
如果您不关心要查找的文本的大小写,请使用:
grep -ir <text_to_find> <directory>
评论
-
s,你会想传入 --
首先 grep;否则会引起有趣的副作用!
如果你严格想使用,那么使用:find
find + grep
find /path/to/somewhere/ -type f -exec grep -nw 'textPattern' {} \;
步骤:
- 用于搜索文件,
find
- 对所有这些执行。
grep
这使您能够查找文件。find
- 如果只想处理某些文件,请使用:
-name Pattern
grep
find /path/to/somewhere/ -type f -name \*.cpp -exec grep -nw 'textPattern' {} \;
您可以使用不同的选项来改进文件搜索。find
评论
grep -lrnw '/root/Desktop/ipozal' -e 'geolocation'
例如:
- 我的文件夹名称是“ipozal"
- 它放置在“/root/Desktop"
- 我想在它的所有文件上找到这个文本“地理位置"
评论
如果您位于 Git 存储库中,则可以使用:
git grep something
评论
git
.gitignore
git
grep "text-to-find-here" file_name
或
grep "text-to-find-here" directory_path/*
如果要搜索当前目录:
grep "text-to-find-here" *
评论
-r
grep
当您在 Linux 上搜索特定文本时,此 grep 命令将为您提供精确的结果 -
grep -inRsH "Text to be searched" /path/to/dir (it can be '.')
i
代表忽略大小写区别R
代表递归,它还包括符号链接。最好使用“R”而不是“r”n
代表“它将打印行号”。s
代表“禁止错误消息”H
代表“它将打印每个匹配的文件名”
评论
.
-H
-l
grep
fgrep
find
find
man find
find … -exec <cmd> +
比 更易于键入且速度更快。仅当接受任意数量的文件名参数时,它才有效。如果像 Python 或 Ruby 脚本那样启动缓慢,则执行时间的节省尤其大。find … -exec <cmd> \;
<cmd>
<cmd>