簡體   English   中英

如何使用linux命令列出包含特定字符串的文件

[英]How to list files which contain specific string using linux commands

我需要使用 linux 命令從包含特定字符串的目錄中列出文件。

使用命令

grep -inr <string> /path/to/directory

-inr選項包含三個部分:

  • i忽略區分大小寫,
  • n顯示每個匹配結果的行號,以及
  • r遞歸讀取每個目錄下的所有文件。

假設您的字符串在環境變量STR並且您在目錄dir/搜索。 你可以這樣做:

find dir/ -type f -name "*${STR}*"

如果搜索包含字符串的文件:

find dir/ -type f -exec grep -l $STR {} \;

但這在這里解釋

你可以做

grep -nr <string> <path to the directory>

這將使用包含字符串的行號打印文件的名稱

如果要使用特定字符串列出文件名,請執行以下操作

find <path to the directory> -iname <string_pattern> -type f

此命令將查找具有匹配字符串的文件。 但是,我添加了更多額外內容,exec 將列出文件。

命令

find <your/dir/> -name "string_to_search*" -type f -exec ls -l {} \;

或者此命令將列出您目錄中的所有 c 文件。

find <your/dir/> -name "*.c" -type f -exec ls -l {} \;

使用 find 命令在文件中查找匹配的字符串。

find  -type f | xargs grep 'text-to-find-here'

這里/表示在所有目錄中搜索

find / -type f | xargs grep 'text-to-find-here'

grep -r "string to be searched"  /path/to/dir

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM