如何使用grep
和find
命令查找包含特定文本的所有文件?
默认情况下,Grep
返回包含某个字符串的文件的所有行。
使用-l
选项可以更改此行为,该选项指示grep
仅返回包含指定文本的文件名。
在文件 document1.txt 和 document2.txt中查找单词example:
$grep -l example document1.txt document2.txt
还可以使用通配符指定在哪些文件中查找:
$grep -l example *.txt
使用-r
(递归)选项,可以在目录(子目录)中的文件中查找:
$grep -lr example /path/to/directory1/*.txt /path/to/directory2
或者省略命令末尾的路径,在当前目录及其所有子目录中搜索。
$grep -lr example
使用-i
选项,在搜索时不区分大小写。
使用find命令搜索特定文本
find和grep配合使用,语法如下:
$find /path/to/search -type f -exec grep -l "your-search-string" {} \;
日期:2020-07-07 20:56:07 来源:oir作者:oir