查找包含文件名中的字符串和文件中其他字符串的文件?

用户名

我想(递归)找到所有文件名中带有“ 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何知道文件名在 Firebase 云存储中是否包含其他字符串?

在Linux终端中查找和删除文件名中包含相同字符串的文件

查找文件名中包含字符串的文件并解压缩

检查文件名中是否包含字符串

使用find和grep从文件中查找包含某些字符串但不包含其他字符串的行

查找包含字符串的文件名的函数

查找不包含指定字符串的文件名

删除文件名的字符串,并将其他字符串保留在BASH中

在 C# 中获取文件名和文件类型作为单独的字符串

批处理:用循环替换文件和文件名中的字符串

使用文件中的行来产生字符串和文件名

查找文件名中包含空格和特定字符串的文件,并将其重命名

如何检查bash中是否存在文件名中包含特定字符串的文件?

Cygwin/Linux - 从文件中查找包含字符串的文件

在字符串变量中包括行号和文件名

打开路径和文件名存储在字符串变量中的工作簿

在文件名的末尾与字符串匹配的目录中查找最后一个文件

使用Python zipfile从ZIP中提取文件名中包含特定字符串的文件

如何从包含绝对文件路径的字符串中获取文件名?

bash cat所有文件名中包含特定字符串的文件

使用文件名读取最新文件包含python中的字符串

如何使用包含在字符串中的文件名使用 Python 打开文件?

如何匹配文件名中的字符串并根据字符串重命名文件?

递归“查找”文件名,该文件名包含另一个目录中的字符串和符号链接文件

Python:文件名包含字符串(元字符?)

使用批处理文件将文件名和文件中的特定字符串传递到for循环中

使用 powershell 在文件名中附加字符串

在 Rust 中以字符串形式获取文件名

使用python在文件名中的字符串