如何使用linux命令

时间:2017-06-13 11:52:29

标签: linux search

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

4 个答案:

答案 0 :(得分:0)

你可以做到

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

这将打印包含字符串

的行号的文件名

如果要列出具有特定字符串的文件名,请执行

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

答案 1 :(得分:0)

假设您的字符串位于环境变量STR中,并在目录dir/中搜索。你可以这样做:

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

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

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

但这解释为here

答案 2 :(得分:0)

此命令将查找具有匹配字符串的文件。 但是,我添加了额外的内容,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

答案 3 :(得分:0)

使用命令

grep -inr <string> /path/to/directory
i -  Ignore case sensitivity.
n -  Display the matched lines and their line numbers.
r -  Read all files under each directory, recursively
相关问题