Linux:在目录下的文件列表中搜索特定单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9665292/
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 05:06:43  来源:igfitidea点击:

Linux : Search for a Particular word in a List of files under a directory

linux

提问by Pawan

I have a big list of log files in a particular directory , related to my java Application under my Linux Remote Servers .

我在特定目录中有一个很大的日志文件列表,这些文件与我的 Linux 远程服务器下的 java 应用程序相关。

When i do ls on that particular directory it shows a list of files (nearly 100 files )

当我在该特定目录上执行 ls 时,它会显示一个文件列表(近 100 个文件)

Now in that List of files , i need to find out a particular word , please tell me , how can i do this ??

现在在那个文件列表中,我需要找出一个特定的词,请告诉我,我该怎么做??

The problem is that I cannot open each and every file and search for that word using /

问题是我无法打开每个文件并使用 / 搜索该词

Please tell me how can i search for a word in the list of files provided .

请告诉我如何在提供的文件列表中搜索单词。

采纳答案by cornuz

grepis made for this.

grep是为此而生的。

Use grep myword *

grep myword *

And check man grep.

并检查man grep

回答by dani herrera

You are looking for grepcommand.

您正在寻找grep命令。

You can read 15 Practical Grep Command Examples In Linux / UNIXfor some samples.

您可以阅读Linux / UNIX 中的 15 个实用 Grep 命令示例以获取一些示例。

回答by joidegn

This is a very frequent task in linux. I use grep -rn '' . all the time to do this. -r for recursive (folder and subfolders) -n so it gives the line numbers, the dot stands for the current directory.

这是 linux 中非常常见的任务。我使用 grep -rn '' 。一直这样做。-r 表示递归(文件夹和子文件夹) -n 所以它给出了行号,点代表当前目录。

grep -rn '<word or regex>' <location>

do a

做一个

man grep 

for more options

更多选择

回答by Whoami

also you can try the following.

您也可以尝试以下方法。

find . -name '*.java' -exec grep "<yourword" /dev/null {} \;

It gets all the files with .java extension and searches 'yourword' in each file, if it presents, it lists the file.

它获取所有扩展名为 .java 的文件并在每个文件中搜索“你的词”,如果出现,则列出该文件。

Hope it helps :)

希望能帮助到你 :)

回答by Ashkan

You can use this command:

你可以使用这个命令:

grep -rn "string" *

n for showing line number with the filename r for recursive

n 用于显示带有文件名的行号 r 用于递归

回答by Abhineet Prasad

You could club find with exec as follows to get the list of the files as well as the occurrence of the word/string that you are looking for

您可以按如下方式使用 exec 查找文件以获取文件列表以及您要查找的单词/字符串的出现次数

find . -exec grep "my word" '{}' \; -print

回答by Ayush Shukla

use this command grep "your word" searchDirectory/*.log

使用这个命令 grep "your word" searchDirectory/*.log

Get more on this link

在此链接上获取更多信息

http://www.cyberciti.biz/faq/howto-recursively-search-all-files-for-words/

http://www.cyberciti.biz/faq/howto-recursively-search-all-files-for-words/