我想(递归地)找到文件名中带有“ABC”的所有文件,这些文件在文件中也包含“XYZ”。我试过:
find . -name "*ABC*" | grep -R 'XYZ'
Run Code Online (Sandbox Code Playgroud)
但它没有给出正确的输出。
ter*_*don 30
那是因为grep
无法从标准输入读取文件名进行搜索。你在做什么是打印文件名包含XYZ
。改用find
's-exec
选项:
find . -name "*ABC*" -exec grep -H 'XYZ' {} +
Run Code Online (Sandbox Code Playgroud)
来自man find
:
-exec command ;
Execute command; true if 0 status is returned. All following
arguments to find are taken to be arguments to the command until
an argument consisting of `;' is encountered. The string `{}'
is replaced by the current file name being processed everywhere
it occurs in the arguments to the command, not just in arguments
where it is alone, as in some versions of find.
[...]
-exec command {} +
This variant of the -exec action runs the specified command on
the selected files, but the command line is built by appending
each selected file name at the end; the total number of invoca?
tions of the command will be much less than the number of
matched files. The command line is built in much the same way
that xargs builds its command lines. Only one instance of `{}'
is allowed within the command. The command is executed in the
starting directory.
Run Code Online (Sandbox Code Playgroud)
如果您不需要实际匹配的行,而只需要包含至少一次出现的字符串的文件名列表,请改用:
find . -name "*ABC*" -exec grep -l 'XYZ' {} +
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
122361 次 |
最近记录: |