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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16956810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:08:38  来源:igfitidea点击:

How do I find all files containing specific text on Linux?

linuxtextgrepdirectoryfind

提问by Nathan

I'm trying to find a way to scan my entire Linux system for all files containing a specific string of text. Just to clarify, I'm looking for text within the file, not in the file name.

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

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.

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

采纳答案by rakib_

Do the following:

请执行下列操作:

grep -rnw '/path/to/somewhere/' -e 'pattern'
  • -ror -Ris recursive,
  • -nis line number, and
  • -wstands for match the whole word.
  • -l(lower-case L) can be added to just give the file name of matching files.
  • -r或者-R是递归的,
  • -n是行号,并且
  • -w代表匹配整个单词。
  • -l(小写 L) 可以添加只给出匹配文件的文件名。

Along with these, --exclude, --include, --exclude-dirflags could be used for efficient searching:

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

  • This will only search through those files which have .c or .h extensions:

    grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
    
  • This will exclude searching all the files ending with .o extension:

    grep --exclude=*.o -rnw '/path/to/somewhere/' -e "pattern"
    
  • For directories it's possible to exclude a particular directory(ies) through --exclude-dirparameter. 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"
    
  • 这只会搜索那些具有 .c 或 .h 扩展名的文件:

    grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
    
  • 这将排除搜索所有以 .o 扩展名结尾的文件:

    grep --exclude=*.o -rnw '/path/to/somewhere/' -e "pattern"
    
  • 对于目录,可以通过--exclude-dir参数排除特定目录。例如,这将排除目录 dir1/、dir2/ 以及所有与 *.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

回答by fedorqui 'SO stop harming'

You can use grep -ilR:

您可以使用grep -ilR

grep -Ril "text-to-find-here" /
  • istands for ignore case (optional in your case).
  • Rstands for recursive.
  • lstands for "show the file name, not the result itself".
  • /stands for starting at the root of your machine.
  • i代表忽略大小写(在您的情况下可选)。
  • R代表递归。
  • l代表“显示文件名,而不是结果本身”。
  • /代表从机器的根开始。

回答by Stephan

You can use ack. It is like grepfor source code. You can scan your entire file system with it.

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

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 .gitignorefile.

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

回答by A R

You can use this:

你可以使用这个:

grep -inr "Text" folder/to/be/searched/

回答by lkamal

List of file names containing a given text

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

First of all, I believe you have used -Hinstead of -l. Also you can try adding the text inside quotes followed by {} \.

首先,我相信你已经使用了-H代替-l。您也可以尝试在引号内添加文本,后跟{} \.

find / -type f -exec grep -l "text-to-find-here" {} \; 

Example

例子

Let's say you are searching for files containing specific text "Apache License" inside your directory. It will display results somewhat similar to below (output will be different based on your directory content).

假设您正在目录中搜索包含特定文本“Apache 许可证”的文件。它将显示类似于下面的结果(输出将根据您的目录内容而有所不同)。

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$ 

Remove case sensitiveness

删除区分大小写

Even if you are not use about the case like "text" vs "TEXT", you can use the -iswitch to ignore case. You can read further details here.

即使您不使用诸如“text”与“TEXT”之类的案例,您也可以使用-i开关来忽略大小写。您可以在此处阅读更多详细信息。

Hope this helps you.

希望这对你有帮助。

回答by Dilawar

I wrote a Python scriptwhich does something similar. This is how one should use this script.

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

./sniff.py path pattern_to_search [file_pattern]

The first argument, path, is the directory in which we will search recursively. The second argument, pattern_to_search, is a regular expression which we want to search in a file. We use the regular expression format defined in the Pythonrelibrary. In this script, the .also matches newline.

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

The third argument, file_pattern, is optional. This is another regular expression which works on a filename. Only those files which matches this regular expression will be considered.

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

For example, if I want to search Python files with the extension pycontaining Pool(followed by word Adaptor, I do the following,

例如,如果我想搜索扩展名py包含Pool(后跟 word 的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 

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.

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

回答by Dilawar

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

e.g.:

例如:

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;

e.g.:

例如:

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

回答by Atul Arvind

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

回答by learner_19

You can use:

您可以使用:

grep -r "string to be searched"  /path/to/dir

The rstands for recursive and so will search in the path specified and also its sub-directories. This will tell you the file name as well as print out the line in the file where the string appears.

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

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 searchn: line number will be shown for matches

除了这个命令,我们也可以这样写: grep -rn "String to search" /path/to/directory/or/file -r: recursive search n: 行号将显示匹配

回答by Alex Jasmin

grepcan 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, i.e. those containing only printable characters.

将打印出所有文本文件的路径,即那些只包含可打印字符的文件。