[英]How to find all files containing specific text (string) on Linux?
How do I find all files containing a specific string of text within their file contents?如何找到文件内容中包含特定文本字符串的所有文件?
The following doesn't work.以下不起作用。 It seems to display every single file in the system.
它似乎显示了系统中的每个文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
Do the following:请执行下列操作:
grep -rnw '/path/to/somewhere/' -e 'pattern'
-r
or -R
is recursive, -r
或-R
是递归的,-n
is line number, and -n
是行号,并且-w
stands for match the whole word. -w
代表匹配整个单词。-l
(lower-case L) can be added to just give the file name of matching files. -l
(小写 L) 可以添加,只给出匹配文件的文件名。-e
is the pattern used during the search -e
是搜索过程中使用的模式Along with these, --exclude
, --include
, --exclude-dir
flags could be used for efficient searching:除了这些,--
--exclude
、-- --include
、-- --exclude-dir
标志可用于高效搜索:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern"
--exclude-dir
parameter.--exclude-dir
参数排除一个或多个目录。 For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"
This works very well for me, to achieve almost the same purpose like yours.这对我来说非常有效,可以实现与您几乎相同的目的。
For more options check man grep
.有关更多选项,请检查
man grep
。
You can use grep -ilR
:您可以使用
grep -ilR
:
grep -Ril "text-to-find-here" /
i
stands for ignore case (optional in your case). i
代表忽略大小写(在您的情况下可选)。R
stands for recursive. R
代表递归。l
stands for "show the file name, not the result itself". l
代表“显示文件名,而不是结果本身”。/
stands for starting at the root of your machine. /
代表从机器的根目录开始。You can use ack .您可以使用ack 。 It is like grep for source code.
它就像源代码的grep 。 You can scan your entire file system with it.
您可以使用它扫描整个文件系统。
Just do:做就是了:
ack 'text-to-find-here'
In your root directory.在您的根目录中。
You can also use regular expressions , specify the filetype, etc.您还可以使用正则表达式、指定文件类型等。
UPDATE更新
I just discovered The Silver Searcher , which is like ack but 3-5x faster than it and even ignores patterns from a .gitignore
file.我刚刚发现了The Silver Searcher ,它类似于 ack 但比它快 3-5 倍,甚至会忽略
.gitignore
文件中的模式。
You can use:您可以使用:
grep -r "string to be searched" /path/to/dir
The r
stands for recursive and so will search in the path specified and also its sub-directories. r
代表递归,因此将在指定的路径及其子目录中进行搜索。 This will tell you the file name as well as print out the line in the file where the string appears.这将告诉您文件名并打印出文件中出现字符串的行。
Or a command similar to the one you are trying (example: ) for searching in all javascript files (*.js):或者类似于您正在尝试的命令(例如:)用于在所有 javascript 文件 (*.js) 中搜索:
find . -name '*.js' -exec grep -i 'string to search for' {} \; -print
This will print the lines in the files where the text appears, but it does not print the file name.这将打印文件中出现文本的行,但不会打印文件名。
In addition to this command, we can write this too: grep -rn "String to search" /path/to/directory/or/file -r: recursive search n: line number will be shown for matches除了这个命令,我们也可以这样写: grep -rn "String to search" /path/to/directory/or/file -r: recursive search n: 行号将显示匹配
你可以使用这个:
grep -inr "Text" folder/to/be/searched/
grep
( GNU or BSD ) grep
( GNU或BSD ) You can use grep
tool to search recursively the current folder, like:您可以使用
grep
工具递归搜索当前文件夹,例如:
grep -r "class foo" .
Note: -r
- Recursively search subdirectories.注意:
-r
- 递归搜索子目录。
You can also use globbing syntax to search within specific files such as:您还可以使用通配语法在特定文件中进行搜索,例如:
grep "class foo" **/*.c
Note: By using globbing option ( **
), it scans all the files recursively with specific extension or pattern.注意:通过使用globbing 选项(
**
),它以特定的扩展名或模式递归扫描所有文件。 To enable this syntax, run: shopt -s globstar
.要启用此语法,请运行:
shopt -s globstar
。 You may also use **/*.*
for all files (excluding hidden and without extension) or any other pattern.您还可以将
**/*.*
用于所有文件(不包括隐藏和无扩展名)或任何其他模式。
If you've the error that your argument is too long, consider narrowing down your search, or use find
syntax instead such as:如果您的参数太长,请考虑缩小搜索范围,或改用
find
语法,例如:
find . -name "*.php" -execdir grep -nH --color=auto foo {} ';'
Alternatively, use ripgrep
.或者,使用
ripgrep
。
ripgrep
If you're working on larger projects or big files, you should use ripgrep
instead, like:如果您正在处理较大的项目或大文件,则应改用
ripgrep
,例如:
rg "class foo" .
Checkout the docs, installation steps or source code on the GitHub project page .查看GitHub 项目页面上的文档、安装步骤或源代码。
It's much quicker than any other tool like GNU / BSD grep
, ucg
, ag
, sift
, ack
, pt
or similar, since it is built on top of Rust's regex engine which uses finite automata, SIMD and aggressive literal optimizations to make searching very fast.它比任何其他工具(如GNU / BSD
grep
、 ucg
、 ag
、 sift
、 ack
、 pt
或类似工具)要快得多,因为它建立在Rust 的正则表达式引擎之上,该引擎使用有限自动机、SIMD 和积极的文字优化来使搜索非常快.
It supports ignore patterns specified in .gitignore
files, so a single file path can be matched against multiple glob patterns simultaneously.它支持
.gitignore
文件中指定的忽略模式,因此单个文件路径可以同时与多个 glob 模式匹配。
You can use common parameters such as:您可以使用常用参数,例如:
-i
- Insensitive searching. -i
- 不敏感的搜索。-I
- Ignore the binary files. -I
- 忽略二进制文件。-w
- Search for the whole words (in the opposite of partial word matching). -w
- 搜索整个单词(与部分单词匹配相反)。-n
- Show the line of your match. -n
- 显示您的匹配行。-C
/ --context
(eg -C5
) - Increases context, so you see the surrounding code. -C
/ --context
(如-C5
) -增加背景下,让你看到周围的代码。--color=auto
- Mark up the matching text. --color=auto
- 标记匹配的文本。-H
- Displays filename where the text is found. -H
- 显示找到文本的文件名。-c
- Displays count of matching lines. -c
- 显示匹配行的计数。 Can be combined with -H
.-H
结合使用。First of all, I believe you have used -H
instead of -l
.首先,我相信您已经使用
-H
而不是-l
。 Also you can try adding the text inside quotes followed by {} \\
.您也可以尝试在引号内添加文本,后跟
{} \\
。
find / -type f -exec grep -l "text-to-find-here" {} \;
Let's say you are searching for files containing specific text "Apache License" inside your directory.假设您正在目录中搜索包含特定文本“Apache 许可证”的文件。 It will display results somewhat similar to below (output will be different based on your directory content).
它将显示类似于下面的结果(输出将根据您的目录内容而有所不同)。
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$
Even if you are not use about the case like "text" vs "TEXT", you can use the -i
switch to ignore case.即使您不使用诸如“text”与“TEXT”之类的案例,您也可以使用
-i
开关来忽略大小写。 You can read further details here .您可以在此处阅读更多详细信息。
Hope this helps you.希望这对你有帮助。
This grep command will give you a precise result when you are searching for specific text on Linux -当您在 Linux 上搜索特定文本时,此 grep 命令将为您提供精确的结果 -
grep -inRsH "Text to be searched" /path/to/dir (it can be '.')
i
stands for ignore case distinctions i
代表忽略大小写区别
R
stands for recursive and it also include symlinks. R
代表递归,它还包括符号链接。 It is better to use 'R' instead of 'r'最好使用“R”而不是“r”
n
stands for "it will print line number". n
代表“它将打印行号”。
s
stands for "suppress error messages" s
代表“抑制错误消息”
H
stands for "it will print the file name for each match" H
代表“它将打印每个匹配的文件名”
If your grep
doesn't support recursive search, you can combine find
with xargs
:如果您的
grep
不支持递归搜索,您可以将find
与xargs
结合使用:
find / -type f | xargs grep 'text-to-find-here'
I find this easier to remember than the format for find -exec
.我发现这比
find -exec
的格式更容易记住。
This will output the filename and the content of the matched line, eg这将输出文件名和匹配行的内容,例如
/home/rob/file:text-to-find-here
Optional flags you may want to add to grep
:您可能想要添加到
grep
可选标志:
-i
- case insensitive search -i
- 不区分大小写的搜索-l
- only output the filename where the match was found -l
- 只输出找到匹配的文件名-h
- only output the line which matched (not the filename) -h
- 只输出匹配的行(不是文件名)grep -insr "pattern" *
i
: Ignore case distinctions in both the PATTERN and the input files. i
:忽略 PATTERN 和输入文件中的大小写区别。n
: Prefix each line of output with the 1-based line number within its input file. n
:在其输入文件中使用从 1 开始的行号为每一行输出添加前缀。s
: Suppress error messages about nonexistent or unreadable files. s
:抑制关于不存在或不可读文件的错误消息。r
: Read all files under each directory, recursively. r
:递归读取每个目录下的所有文件。There's a new utility called The Silversearcher有一个名为The Silversearcher的新实用程序
sudo apt install silversearcher-ag
It works closely with Git and other VCS.它与 Git 和其他 VCS 密切配合。 So you won't get anything in a .git or another directory.
所以你不会在.git或其他目录中得到任何东西。
You can simply use你可以简单地使用
ag "Search query"
And it will do the task for you!它会为你完成任务!
How do I find all files containing specific text on Linux?
如何在 Linux 上查找包含特定文本的所有文件? (...)
(……)
I came across this solution twice:
我两次遇到这个解决方案:
find / -type f -exec grep -H 'text-to-find-here' {} \\;
If using find like in your example, better add -s
( --no-messages
) to grep
, and 2>/dev/null
at the end of the command to avoid lots of Permission denied messages issued by grep
and find
:如果在您的示例中使用find ,最好将
-s
( --no-messages
) 添加到grep
,并在命令末尾添加2>/dev/null
以避免grep
和find
发出大量权限被拒绝的消息:
find / -type f -exec grep -sH 'text-to-find-here' {} \; 2>/dev/null
find is the standard tool for searching files - combined with grep when looking for specific text - on Unix-like platforms. find是在类 Unix 平台上搜索文件的标准工具 - 在查找特定文本时与 grep 结合使用。 The find command is often combined with xargs , by the way.
顺便说一下, find命令通常与xargs结合使用。
Faster and easier tools exist for the same purpose - see below.出于同样的目的,存在更快、更简单的工具 - 见下文。 Better try them, provided they're available on your platform , of course:
最好尝试它们,当然,前提是它们在您的平台上可用:
RipGrep - fastest search tool around: RipGrep - 最快的搜索工具:
rg 'text-to-find-here' / -l
ag 'text-to-find-here' / -l
ack 'text-to-find-here' / -l
Note: You can add 2>/dev/null
to these commands as well, to hide many error messages.注意:您也可以将
2>/dev/null
添加到这些命令中,以隐藏许多错误消息。
Warning : unless you really can't avoid it, don't search from '/' (the root directory) to avoid a long and inefficient search!警告:除非您真的无法避免,否则不要从“/” (根目录)进行搜索,以免搜索时间过长且效率低下! So in the examples above, you'd better replace ' / ' by a sub-directory name, eg "/home" depending where you actually want to search...
所以在上面的例子中,你最好用子目录名称替换“ / ”,例如“/home”,具体取决于你实际想要搜索的位置......
尝试:
find . -name "*.txt" | xargs grep -i "text_pattern"
Use pwd
to search from any directory you are in, recursing downward使用
pwd
从您所在的任何目录中搜索,向下递归
grep -rnw `pwd` -e "pattern"
Update Depending on the version of grep you are using, you can omit pwd
.更新根据您使用的 grep 版本,您可以省略
pwd
。 On newer versions .
在较新的版本上
.
seems to be the default case for grep if no directory is given thus:如果没有给出目录,则似乎是 grep 的默认情况:
grep -rnw -e "pattern"
or或者
grep -rnw "pattern"
will do the same thing as above!会做和上面一样的事情!
grep
can be used even if we're not looking for a string.即使我们不寻找字符串,也可以使用
grep
。
Simply running,简单的跑步,
grep -RIl "" .
will print out the path to all text files, ie those containing only printable characters.将打印出所有文本文件的路径,即那些只包含可打印字符的文件。
Silver Searcher is a terrific tool, but ripgrep may be even better. Silver Searcher 是一个了不起的工具,但 ripgrep 可能更好。
It works on Linux, Mac and Windows, and was written up on Hacker News a couple of months ago (this has a link to Andrew Gallant's Blog which has a GitHub link):它适用于 Linux、Mac 和 Windows,几个月前在Hacker News上写过(这有一个链接到 Andrew Gallant 的博客,其中有一个 GitHub 链接):
Ripgrep – A new command line search tool Ripgrep - 一个新的命令行搜索工具
If you strictly want to use find
then use find + grep
:如果您严格想使用
find
则使用find + grep
:
find /path/to/somewhere/ -type f -exec grep -nw 'textPattern' {} \\;
Steps :步骤:
find
to search files,find
来搜索文件,grep
on all of them.grep
。 This gives you the power of find
to find files.这使您
find
使用 find 查找文件。
-name Pattern
if you want to grep
only certain files:grep
某些文件,请使用-name Pattern
: find /path/to/somewhere/ -type f -name \\*.cpp -exec grep -nw 'textPattern' {} \\;
You can use different options of find
to improve your file search.您可以使用不同的
find
选项来改进文件搜索。
Here are the several list of commands that can be used to search file.以下是可用于搜索文件的几个命令列表。
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
I am fascinated by how simple grep makes it with 'rl':我对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'
Use '-r' without 'l' to see the file names followed by text in which the pattern is found !
使用不带 'l' 的 '-r' 来查看文件名后跟找到模式的文本!
grep -r 'pattern_to_find' /path/where/to/find
It works just perfect...它工作得非常完美......
find /path -type f -exec grep -l "string" {} \;
Explanation from comments来自评论的解释
find is a command that lets you find files and other objects like directories and links in subdirectories of a given path. find 是一个命令,可让您在给定路径的子目录中查找文件和其他对象,如目录和链接。 If you don't specify a mask that filesnames should meet, it enumerates all directory objects.
如果您没有指定文件名应满足的掩码,它将枚举所有目录对象。
-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
Hope this is of assistance...希望这是有帮助的...
Expanding the grep
a bit to give more information in the output, for example, to get the line number in the file where the text is can be done as follows:稍微扩展
grep
以在输出中提供更多信息,例如,获取文本所在文件中的行号,如下所示:
find . -type f -name "*.*" -print0 | xargs --null grep --with-filename --line-number --no-messages --color --ignore-case "searthtext"
And if you have an idea what the file type is you can narrow your search down by specifying file type extensions to search for, in this case .pas
OR .dfm
files:如果您知道文件类型是什么,您可以通过指定要搜索的文件类型扩展名来缩小搜索范围,在本例中为
.pas
或.dfm
文件:
find . -type f \( -name "*.pas" -o -name "*.dfm" \) -print0 | xargs --null grep --with-filename --line-number --no-messages --color --ignore-case "searchtext"
Short explanation of the options:选项的简短说明:
.
in the find
specifies from the current directory.find
指定从当前目录。-name
" *.*
" : for all files ( -name " *.pas
" -o -name " *.dfm
" ) : Only the *.pas
OR *.dfm
files, OR specified with -o
-name
" *.*
" : 对于所有文件 ( -name " *.pas
" -o -name " *.dfm
" ) : 只有*.pas
OR *.dfm
文件,或者用-o
指定-type f
specifies that you are looking for files -type f
指定您正在寻找文件-print0
and --null
on the other side of the |
-print0
和--null
在|
的另一侧(pipe) are the crucial ones, passing the filename from the find
to the grep
embedded in the xargs
, allowing for the passing of filenames WITH spaces in the filenames, allowing grep to treat the path and filename as one string, and not break it up on each space. find
传递到嵌入在xargs
的grep
,允许在文件名中传递带有空格的文件名,允许 grep 将路径和文件名视为一个字符串,而不是破坏它在每个空间上。A Simple find
can work handy.一个简单的
find
可以很方便地工作。 alias it in your ~/.bashrc
file:在你的
~/.bashrc
文件中给它别名:
alias ffind find / -type f | xargs grep
Start a new terminal and issue:启动一个新终端并发出:
ffind 'text-to-find-here'
grep -lrnw '/root/Desktop/ipozal' -e 'geolocation'
For example:例如:
I wrote a Python script which does something similar.我写了一个Python 脚本,它做类似的事情。 This is how one should use this script.
这就是人们应该如何使用这个脚本。
./sniff.py path pattern_to_search [file_pattern]
The first argument, path
, is the directory in which we will search recursively.第一个参数
path
是我们将递归搜索的目录。 The second argument, pattern_to_search
, is a regular expression which we want to search in a file.第二个参数
pattern_to_search
是我们要在文件中搜索的正则表达式。 We use the regular expression format defined in the Python re
library.我们使用Python
re
库中定义的正则表达式格式。 In this script, the .
在这个脚本中,
.
also matches newline.也匹配换行符。
The third argument, file_pattern
, is optional.第三个参数
file_pattern
是可选的。 This is another regular expression which works on a filename.这是另一个适用于文件名的正则表达式。 Only those files which matches this regular expression will be considered.
仅考虑与此正则表达式匹配的那些文件。
For example, if I want to search Python files with the extension py
containing Pool(
followed by word Adaptor
, I do the following,例如,如果我想搜索包含
Pool(
后跟单词Adaptor
的扩展名py
Python 文件,我执行以下操作,
./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
And voila, it generates the path of matched files and line number at which the match was found.瞧,它生成匹配文件的路径和找到匹配的行号。 If more than one match was found, then each line number will be appended to the filename.
如果找到多个匹配项,则将每个行号附加到文件名。
Try:尝试:
find / -type f -exec grep -H 'text-to-find-here' {} \;
which will search all file systems, because /
is the root folder.这将搜索所有文件系统,因为
/
是根文件夹。
For home folder use:对于主文件夹使用:
find ~/ -type f -exec grep -H 'text-to-find-here' {} \;
For current folder use:对于当前文件夹使用:
find ./ -type f -exec grep -H 'text-to-find-here' {} \;
There is an ack
tool that would do exactly what you are looking for.有一个
ack
工具可以完全满足您的要求。
http://linux.die.net/man/1/ack http://linux.die.net/man/1/ack
ack -i search_string folder_path/*
You may ignore -i
for case sensitive search您可以忽略
-i
进行区分大小写的搜索
Use:用:
grep -c Your_Pattern *
This will report how many copies of your pattern are there in each of the files in the current directory.这将报告当前目录中的每个文件中有多少个模式副本。
To search for the string and output just that line with the search string:要搜索字符串并仅输出带有搜索字符串的那一行:
for i in $(find /path/of/target/directory -type f); do grep -i "the string to look for" "$i"; done
eg:例如:
for i in $(find /usr/share/applications -type f); \
do grep -i "web browser" "$i"; done
To display filename containing the search string:显示包含搜索字符串的文件名:
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;
eg:例如:
for i in $(find /usr/share/applications -type f); \
do if grep -i "web browser" "$i" > /dev/null; then echo "$i"; \
fi; done;
All previous answers suggest grep and find.以前的所有答案都建议使用 grep 和 find。 But there is another way: Use Midnight Commander
但还有另一种方法:使用午夜指挥官
It is a free utility (30 years old, proven by time) which is visual without being GUI.它是一个免费的实用程序(30 岁,已被时间证明),它是可视化的,而不是 GUI。 It has tons of functions, and finding files is just one of them.
它有很多功能,查找文件只是其中之一。
grep "text-to-find-here" file_name
or或者
grep "text-to-find-here" directory_path/*
If you want to search current directory:如果要搜索当前目录:
grep "text-to-find-here" *
You can use below command as you don't want file name but you want to search from all the files.您可以使用以下命令,因为您不想要文件名但想要从所有文件中搜索。 Here are i am capturing "TEXT" form All the log files making sure that file name is not printed
这是我正在捕获“文本”表单所有确保不打印文件名的日志文件
grep -e TEXT *.log | cut -d' ' --complement -s -f1
grep with -e option is quite quick compared to other option as it is for PATTERN match与其他选项相比,带有 -e 选项的 grep 非常快,因为它用于 PATTERN 匹配
以下命令适用于这种方法:
find ./ -name "file_pattern_name" -exec grep -r "pattern" {} \;
I'm trying to find a way to scan my entire Linux system for all files containing a specific string of text.我试图找到一种方法来扫描我整个 Linux 系统中包含特定文本字符串的所有文件。 Just to clarify, I'm looking for text within the file, not in the file name.
澄清一下,我正在文件中查找文本,而不是文件名中的文本。
When I was looking up how to do this, I came across this solution twice:当我在寻找如何做到这一点时,我两次遇到了这个解决方案:
find / -type f -exec grep -H 'text-to-find-here' {} \;
However, it doesn't work.但是,它不起作用。 It seems to display every single file in the system.
它似乎显示了系统中的每个文件。
Is this close to the proper way to do it?这接近正确的方法吗? If not, how should I?
如果没有,我该怎么办? This ability to find text strings in files would be extraordinarily useful for some programming projects I'm doing.
这种在文件中查找文本字符串的能力对于我正在做的一些编程项目非常有用。
Try this:尝试这个:
find / -type f -name "*" -exec grep -il "String_to_search" {} \;
Or或者
for i in /*;do grep -Ril "String_to_search" $i;done 2> /dev/null
试试这个:
find . | xargs grep 'word' -sl
Use:用:
grep -Erni + "text you wanna search"
The command will search recursively in all files and directories of the current directory and print the result.该命令将在当前目录的所有文件和目录中递归搜索并打印结果。
Note: if your grep output isn't colored, you can change it by using the grep='grep --color=always' alias in your shell source file.注意:如果您的 grep 输出没有着色,您可以通过在 shell 源文件中使用 grep='grep --color=always' 别名来更改它。
find
with xarg
s is preferred when there are many potential matches to sift through.当有许多潜在的匹配要筛选时,使用
xarg
s 的find
是首选。 It runs more slowly than other options, but it always works.它比其他选项运行得更慢,但它始终有效。 As some have discovered,
xargs
does not handle files with embedded spaces by default.正如一些人发现的那样,默认情况下
xargs
不处理带有嵌入空格的文件。 You can overcome this by specifying the -d
option.您可以通过指定
-d
选项来克服这个问题。
Here is @RobEarl's answer, enhanced so it handles files with spaces:这是@RobEarl 的答案,经过增强,可以处理带空格的文件:
find / -type f | xargs -d '\n' grep 'text-to-find-here'
Here is @venkat's answer, similarly enhanced:这是@venkat 的答案,同样增强:
find . -name "*.txt" | xargs -d '\n' grep -i "text_pattern"
Here is @Gert van Biljon's answer, similarly enhanced:这是@Gert van Biljon 的回答,同样加强了:
find . -type f -name "*.*" -print0 | xargs -d '\n' --null grep --with-filename --line-number --no-messages --color --ignore-case "searthtext"
Here is @LetalProgrammer's answer, similarly enhanced:这是@LetalProgrammer 的回答,同样得到了增强:
alias ffind find / -type f | xargs -d '\n' grep
Here is @Tayab Hussain's answer, similarly enhanced:这是@Tayab Hussain 的回答,同样加强了:
find . | xargs -d '\n' grep 'word' -sl
If you have a set of files that you will always be checking you can alias their paths, for example:如果您有一组将始终检查的文件,您可以为它们的路径设置别名,例如:
alias fd='find . -type f -regex ".*\.\(inc\|info\|module\|php\|test\|install\|uninstall\)"'
Then you can simply filter the list like this:然后你可以像这样简单地过滤列表:
grep -U -l $'\015' $(fd)
Which filters out the list fd to files that contain the CR pattern.它将列表 fd 过滤为包含 CR 模式的文件。
I find that aliasing the files that I am interested in helps me create easier scripts then always trying to remember how to get all those files.我发现为我感兴趣的文件设置别名有助于我创建更简单的脚本,然后总是试图记住如何获取所有这些文件。 The recursive stuff works as well but sooner or later you are going to have to contend with weeding out specific file types.
递归的东西也有效,但迟早你将不得不与清除特定的文件类型作斗争。 Which is is why I just find all the file types I'm interested in to begin with.
这就是为什么我只找到我感兴趣的所有文件类型的原因。
您可以使用以下命令从文件中查找特定文本:
cat file | grep 'abc' | cut -d':' -f2
尝试这个
find . -type f -name some_file_name.xml -exec grep -H PUT_YOUR_STRING_HERE {} \;
As Peter in the previous answer mentioned , all previous answers suggest grep and find.正如彼得在上一个答案中提到的,之前的所有答案都建议使用 grep 和 find。
But there is a more sophisticated way using Gnome Commander with a perfect GUI and with tons of options since 2001, and finding files is just one of them.但是,自 2001 年以来,使用Gnome Commander有一种更复杂的方法,它具有完美的GUI和大量选项,而查找文件只是其中之一。 It is a free utility as well, proven by time.
它也是一个免费的实用程序,已经被时间证明了。
See also The Platinium Searcher , which is similar to The Silver Searcher and it's written in Go.另请参阅The Platinium Searcher ,它类似于The Silver Searcher ,它是用 Go 编写的。
Example:例子:
pt -e 'text to search'
GUI Search Alternative - For Desktop Use: GUI 搜索替代方案 - 供桌面使用:
- As the question is not precisely asking for commands - 因为问题并不是精确地要求命令
Searchmonkey : Advanced file search tool without having to index your system using regular expressions. Searchmonkey :高级文件搜索工具,无需使用正则表达式索引您的系统。 Graphical equivalent to find/grep.
图形等效于 find/grep。 Available for Linux (Gnome/KDE/Java) and Windows (Java) - open source GPL v3
适用于 Linux (Gnome/KDE/Java) 和 Windows (Java) - 开源 GPL v3
Features:特征:
Download - Links:下载 - 链接:
. .
Screen-shot:截屏:
I'm trying to find a way to scan my entire Linux system for all files containing a specific string of text.
我试图找到一种方法来扫描我整个 Linux 系统中包含特定文本字符串的所有文件。 ... Is this close to the proper way to do it?
...这接近正确的方法吗? If not, how should I?
如果没有,我该怎么办? ... This ability to find text strings in files would be extraordinarily useful for some programming projects I'm doing.
... 这种在文件中查找文本字符串的能力对于我正在做的一些编程项目非常有用。
While you should never replace (or alias) a system command with a different program, due to risk of mysterious breakage of scripts or other utilities, if you are running a text search manually or from your own scripts or programs you should consider the fastest suitable program when searching a large number of files a number of times.虽然您永远不应该用不同的程序替换(或别名)系统命令,但由于脚本或其他实用程序的神秘损坏风险,如果您手动或从您自己的脚本或程序运行文本搜索,您应该考虑最快的合适的程序在多次搜索大量文件时使用。 Ten minutes to half an hour time spent installing and familiarizing yourself with a better utility can be recovered after a few uses for the use-case you described.
在对您描述的用例进行几次使用后,可以恢复花费十分钟到半小时的时间来安装和熟悉更好的实用程序。
A webpage offering a " Feature comparison of ack, ag, git-grep, GNU grep and ripgrep " can assist you to decide which program offers the features you need.提供“ ack、ag、git-grep、GNU grep 和 ripgrep 的功能比较”的网页可以帮助您确定哪个程序提供了您需要的功能。
Andrew Gallant's Blog claims: " ripgrep is faster than {grep, ag, git grep, ucg, pt, sift} " (a claim shared by some of the others, this is why a feature comparison is helpful). Andrew Gallant 的博客声称:“ ripgrep 比 {grep, ag, git grep, ucg, pt, sift} 快”(其他一些人也有这样的说法,这就是功能比较有用的原因)。 Of particular interest is his section on regex implementations and pitfalls.
特别感兴趣的是他关于正则表达式实现和陷阱的部分。
The following command searches all files, including hidden and executable:以下命令搜索所有文件,包括隐藏文件和可执行文件:
$ rg -uuu foobar
The Silver Searcher (ag) claims it is 5-10x faster than Ack. Silver Searcher (ag) 声称它比 Ack 快 5-10 倍。 This program is suggested in some other answers.
在其他一些答案中建议使用该程序。 The GitHub doesn't appear as recent as ripgrep's and there are noticably more commits and branches with fewer releases, it's hard to draw an absolute claim based on those stats.
GitHub 似乎不像 ripgrep 那样新,并且有明显更多的提交和分支,而发布的版本更少,很难根据这些统计数据得出绝对的主张。 The short version : ripgrep is faster, but there's a tiny learning curve to not get caught by the differences.
简短版本:ripgrep 速度更快,但有一个很小的学习曲线,不会被差异所吸引。
So what could be next, you guessed it, the platinum searcher .那么接下来会是什么,你猜对了,白金搜索者。 The claims are: it searches code about 3–5× faster than ack, but its speed is equal to the silver searcher.
声称是:它搜索代码比 ack 快 3-5 倍,但它的速度等于银搜索器。 It's written in GoLang and searches UTF-8, EUC-JP and Shift_JIS files;
它是用 GoLang 编写的,可以搜索 UTF-8、EUC-JP 和 Shift_JIS 文件; if that's of greater interest.
如果那是更大的兴趣。 The GitHub is neither particularly recent or active.
GitHub 既不是特别新,也不是特别活跃。 GoLang itself has a fast and robust regex, but the platinum searcher would be better recommended if it had a better user interest.
GoLang 本身有一个快速而强大的正则表达式,但如果它有更好的用户兴趣,最好推荐白金搜索器。
For a combination of speed and power indexed query languages such as ElasticSearch or Solr can be a long term investment that pays off , but not if you want a quick and simple replacement for grep.对于速度和强大的索引查询语言(例如ElasticSearch或Solr)的组合,可能是一项值得的长期投资,但如果您想要快速简单地替代 grep,则不是。 OTOH both have an API which can be called from any program you write, adding powerful searches to your program.
OTOH 都有一个可以从您编写的任何程序中调用的 API,为您的程序添加强大的搜索功能。
While it's possible to spawn an external program, execute a search, intercept its output and process it, calling an API is the way to go for power and performance.虽然可以生成外部程序、执行搜索、拦截其输出并对其进行处理,但调用 API 是提高功率和性能的方法。
This question was protected Aug 6 '15 at 19:34 with this caution:
We're looking for long answers that provide some explanation and context.我们正在寻找能够提供一些解释和背景的长篇答案。 Don't just give a one-line answer;
不要只给出一行答案; explain why your answer is right, ideally with citations.
解释为什么你的答案是正确的,最好是引用。
While some answers suggest alternative ways to accomplish a search they don't explain why other than it's "free", "faster", "more sophisticated", "tons of features", etc. Don't try to sell it, just tell us "why your answer is right".虽然一些答案提出了完成搜索的替代方法,但除了“免费”、“更快”、“更复杂”、“大量功能”等之外,它们并没有解释为什么。不要试图出售它,只需告诉我们“为什么你的答案是正确的”。 I've attempted to teach how to choose what's best for the user, and why .
我试图教如何选择最适合用户的东西,以及为什么. This is why I offer yet another answer, when there are already so many.
这就是为什么我提供另一个答案的原因,因为已经有这么多答案了。 Otherwise I'd agree that there are already quite a few answers;
否则我会同意已经有很多答案了; I hope I've brought a lot new to the table.
我希望我带来了很多新东西。
My use case was to find Python code I had written way back that wrote jsonlines a particular way.我的用例是找到我写的 Python 代码,该代码以特定方式编写 jsonlines。 I knew that
jsonl
would be part of the function name and to_json
would appear in the body, but not much else.我知道
jsonl
将是函数名称的一部分,而to_json
将出现在主体中,但to_json
别无其他。
Despite 50 answers, finding more than one string in the same file (whether or not in the same line) hasn't been answered.尽管有 50 个答案,但在同一个文件(无论是否在同一行)中找到多个字符串仍未得到解答。 Hopefully someone else in the same situation finds this answer and can reuse this snippet.
希望处于相同情况的其他人找到这个答案并且可以重用这个片段。
The -q
in grep is for quiet. grep 中的
-q
表示安静。 Nothing is printed, only the return value is set.不打印任何内容,只设置返回值。 Thus the
-print
at the end.因此
-print
最后。 Each -exec
only runs if the previous one succeeded.每个
-exec
仅在前一个成功时运行。 So if you have many files it pays to think about patterns that will eliminate files you aren't interested in.因此,如果您有很多文件,那么考虑可以消除您不感兴趣的文件的模式是值得的。
find . -type f -name "*.py" \
-exec grep -q -e 'to_json' {} \; \
-exec grep -q -e 'def\s.*jsonl' {} \; \
-print
Your command is correct.你的命令是正确的。 You just need to add
-l
to grep:您只需要在 grep 中添加
-l
:
find / -type f -exec grep -l 'text-to-find-here' {} \;
I tried the grep
command below.我尝试了下面的
grep
命令。 It helps searching contents within my repository at /etc/yum.repos.d
.它有助于在我的存储库中搜索内容
/etc/yum.repos.d
。
grep -Ril -e 'texttoSearch' /etc/yum.repos.d
Try this command.试试这个命令。 Which will give you the files containing the pattern you entered.
这将为您提供包含您输入的模式的文件。
sudo grep -inr "your-pattern" /
Here: i - Ignore case distinctions, so that characters that differ only in case match each other.这里: i - 忽略大小写区别,以便仅在大小写不同的字符相互匹配。
n - Make sure that the first character of actual line content lies on a tab stop, so that the alignment of tabs looks normal. n - 确保实际行内容的第一个字符位于制表位上,以便制表符的对齐看起来正常。
r - Read all files under each directory, recursively, following symbolic links only if they are on the command line. r - 递归读取每个目录下的所有文件,仅当它们位于命令行上时才遵循符号链接。 Note that if no file operand is given, grep searches the working directory.
请注意,如果没有给出文件操作数,grep 将搜索工作目录。
You can also use awk
:您还可以使用
awk
:
awk '/^(pattern)/{print}' /path/to/find/*
pattern
is the string you want to match in the files. pattern
是您要在文件中匹配的字符串。
请根据需要自定义以下命令,并从文件中递归查找任何字符串。
grep -i hack $(find /etc/ -type f)
如果您在 git 存储库中,则可以使用:
git grep something
I think it is worth mentioning how you can find:我认为值得一提的是如何找到:
all files containing at least one text, among a big set of texts:在一大组文本中至少包含一个文本的所有文件:
grep -rlf ../patternsFile.txt .
Output:输出:
./file1
./file2
./file4
the above, grouped by each text:以上,按每个文本分组:
cat ../patternsFile.txt | xargs -I{} sh -c "echo {}; grep -rl \"{}\" ."
Output:输出:
pattern1
./file1
./file2
pattern2
./file1
./file4
pattern3
./file1
./file2
./file4
Note that in order not to match patternsFile.txt
itself you need to add it one directory up.请注意,为了不匹配
patternsFile.txt
本身,您需要将其添加一个目录。
查找名称为“ .kube/config
”且内容包括eks_use1d
任何文件:
locate ".kube/config" | xargs -i sh -c 'echo \\n{};cat {} | grep eks_use1d'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.