如何在 Linux 上遞迴搜尋所有檔案中的字串
grep 命令用於搜尋文字或掃描給定的記錄,以查詢包含與給定字串或單詞相匹配的行的行。使用 grep 搜尋與一個或多個正則表示式匹配的文字行,並且僅輸出匹配的行。使用 grep 命令,我們可以在 Linux 上遞迴搜尋所有檔案中的字串。
語法如下所示-
$ grep -r "word"
例如,要在 Downloads 目錄中搜索 “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 文章中瞭解更多關於這些型別的命令。請繼續閱讀!
廣告