在Linux上搜索多个文件中的字符串

时间:2013-06-21 08:58:00

标签: linux search grep

如何在多个文件或文件夹中搜索纯文本文件中的字符串?

例如,我需要在“/ home / thisuser / bar / baz /”

文件夹中的所有文件中找到字符串“foo”

2 个答案:

答案 0 :(得分:10)

您需要对要搜索的文件具有读取权限。如果您有,则只需使用

grep -r "foo" /home/thisuser/bar/baz/*

搜索某个文件夹或

grep "foo" /home/thisuser/bar/baz/somefile.txt

如果您需要搜索特定文件,在本例中为“somefile.txt”。 基本上语法是

grep [options] [searched string] [path]
// -r is an option which states that it will use recursive search

另一个有用的选项是“-n”来显示字符串所在文件的哪一行,“-i”忽略大小写,“ - s”来抑制一些消息,如“无法读取文件”或“找不到“和”-I“忽略二进制文件。

如果您使用

grep -rnisI "foo" /home/thisuser/bar/baz/*

你会知道在哪里看。

答案 1 :(得分:0)

使用

grep -r "foo" /home/thisuser/bar/baz/
相关问题