2017-06-13 100 views
-2

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

+1

包含在文件名或内部搜索?文本文件? –

+0

[如何在Linux上查找包含特定文本的所有文件?](https://stackoverflow.com/questions/16956810/how-do-i-find-all-files-containing-specific-text-on -linux) – xiawi

回答

0

你可以做

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

这将打印包含字符串

如果你想与特定的字符串列表文件名(S)的行号的文件的名称,然后做

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

让我们说你的字符串是在环境变量STR和你在目录中搜索dir/。你可以这样做:

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

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

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

但这种解释here

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 
+0

谢谢,但它不是列出文件 – Deepi

+0

你想列出目录中的文件? – LethalProgrammer

+0

我想列出包含特定字符串的目录中的文件,例如:如果文件包含57690,应该列出 – Deepi

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 
相关问题