为了才能在Linux命令行中找到所有名称包含字符串的文件linux 发邮件,我们将使用grep命令,首先我们必须了解grep命令是哪些以及怎样在Linux上使用它。
Linux中的grep命令用于过滤文件中特定字符模式的搜索。它是最常用的Linux实用程序命令之一,用于显示包含我们尝试搜索的模式的行。
一般,我们尝试在文件中搜索的模式称为正则表达式。
句型
grep [options] pattern [files]
其实我们有好多不同的选择linux使用问号查询字符,但最常用的是-
-c : It lists only a count of the lines that match a pattern -h : displays the matched lines only. -i : Ignores, case for matching -l : prints filenames only -n : Display the matched lines and their line numbers. -v : It prints out all the lines that do not match the pattern -R : stands for recurse, would go into subdirectories as well.
如今,让我们考虑一种情况,我们希望在特定目录的所有文件中找到特定模式,例如dir1。
句型
grep -rni "word" *
在前面的命令中国内linux主机,将“word”占位符替换为
因此,我们使用如下所示的命令-
grep -rni "func main()" *
里面的命令将尝试main()在特定目录和子目录中的所有文件中查找字符串“func”。
输出结果
main.go:120:func main() {}
假如我们只想在单个目录中而不是在子目录中找到特定模式,这么我们须要使用如下所示的命令-
grep -s "func main()" *
在前面的命令中linux使用问号查询字符,我们使用了-s标志,这将帮助我们不对运行命令的目录中存在的每位子目录发出警告。
输出结果
main.go:120:func main() {}
示例命令
grep -R "apples" .
输出结果
immukul@192 linux-dir1% grep -R "apples" . ./d1/file.txt:apples ./d1/file.txt:applesauce ./d1/file.txt:applesnits ./d1/file.txt:dapples ./d1/file.txt:grapples ./d1/file.txt:mayapples ./d1/file.txt:pineapples ./d1/file.txt:sapples ./d1/file.txt:scrapples ./d2/2.txt:orange apples is great together ./d2/2.txt:apples nto great ./d2/2.txt:is apples good