的grep命令用于搜索文本或扫描包含匹配到给定的字符串或单词行给定记录。使用grep搜索与一个或多个正则表达式匹配的文本行,并仅输出匹配的行。使用grep命令,我们可以在Linux上递归地在所有文件中搜索字符串。
的语法如下所示-
$ grep -r "word"
例如,在“下载”目录中搜索“ Linux”一词。命令应该是这样的
~/Downloads$ grep -r "Linux"
样本输出应如下所示–
zookeeper_installation.htm:<li><p><b>Any of Linux OS</b> − Supports development and deployment. It is preferred for demo applications.</p></li> Linux Howtos.html:1. How to add a New Disk Drive to a Linux System? Linux Howtos.html:http://www.yolinux.com/TUTORIALS/LinuxTutorialAdditionalHardDrive.html Linux Howtos.html:2. How to create a new virtual disk for an existing Linux virtual machine? Linux Howtos.html:4. How to Increase the size of a Linux LVM by adding a new disk Linux Howtos.html:5. How to format a Linux Hard Disk? Linux Howtos.html:6. How to partition and format a new drive in Linux System? Linux Howtos.html:7. How to mount NTFS Drives on a Linux System?
要忽略大小写区别,请使用以下命令–
$ grep -ri "linux" .
样本输出应如下所示–
zookeeper_installation.htm:<li><p><b>Any of Linux OS</b> − Supports development and deployment. It is preferred for demo applications.</p></li> zookeeper_installation.htm:<p>The latest version (while writing this tutorial) is JDK 8u 60 and the file is “jdk-8u60-linuxx64.tar.gz”. Please download the file on your machine.</p> zookeeper_installation.htm:$ tar -zxf jdk-8u60-linux-x64.gz
要使用GNU grep显示仅打印文件名,请使用以下命令–
$grep -r -l "linux"
您还可以使用以下命令指定目录名称–
$ grep -r -l "linux" /path/to/dir/*.c
恭喜你!现在,您知道“如何在Linux上递归搜索所有文件以查找字符串”。在我们的下一篇Linux文章中,我们将详细了解这些类型的命令。继续阅读!