繁体   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