gordon's note infotech Find all files containing a specific text (string) on Linux

Find all files containing a specific text (string) on Linux

在目前目錄下,找尋哪些檔案中有 pattern 文字(-r 包含子目錄, 也可以用大寫的 -R

grep -r "pattern" .

 
或者加入 -i 不管大小寫

grep -ri "pattern" .

 
其他還有⋯⋯
-n (line number): 顯示行數(pattern 在第幾行找到的)
-l (files with matches): 只要顯示檔案名稱,其他的資訊都不要(乾淨)
-w (whole word): 比對時要符合完整字串
–include=’*.ext’ (include specific file types): 限制只尋找某些符合條件的檔案
–exclude-dir=”dirname” (exclude specific directories): 跳過某目錄不要尋找
 
例如:

grep -rinl "pattern" /path/to/start_search --exclude-dir="temp"

 
如果要放在別的位置,可以用 -e 指定,例如:

grep -rnw '/path/to/somewhere/' -e 'pattern'

 
 
 
Source: Find all files containing a specific text (string) on Linux
 
 
#find #text #string #terminal

Leave a Reply

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

Related Post