我想(递归)找到所有文件名中带有“ ABC”的文件,该文件中也包含“ XYZ”。我试过了:
find . -name "*ABC*" | grep -R 'XYZ'
但它没有给出正确的输出。
那是因为grep
无法读取文件名以通过标准输入进行搜索。您正在做的是打印包含的文件名XYZ
。使用find
的-exec
选项代替:
find . -name "*ABC*" -exec grep -H 'XYZ' {} +
来自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.
如果您不需要实际的匹配行,而只需要包含至少一个出现的字符串的文件名列表,请改用此命令:
find . -name "*ABC*" -exec grep -l 'XYZ' {} +
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句