使用find命令搜索具有某些文本模式的所有文件

时间:2009-04-07 18:33:31

标签: find command

我使用以下find命令查找并显示具有输入文本模式的所有文件。

找到。 -type f -print | xargs grep -n“pattern”

我有很多项目文件夹,每个文件夹都有自己的makefile,命名为'Makefile'。(没有文件扩展名,只是'Makefile')

如何使用上述命令仅在名为Makefile的文件中搜索特定模式,这些文件存在于我的所有项目文件夹中?

-AD。

7 个答案:

答案 0 :(得分:7)

-print不是必需的(至少通过GNU find实现)。 -name参数允许指定文件名模式。因此命令将是:

find . -name Makefile | xargs grep pattern

答案 1 :(得分:2)

如果目录路径中有空格或奇数字符,则需要使用以null结尾的方法:

 find . -name Makefile -print0 | xargs -0 grep pattern

答案 2 :(得分:1)

find . -type f -name 'Makefile' | xargs egrep -n "pattern"

如果路径很长,请使用egrep

重复:this

答案 3 :(得分:1)

您可以使用xargs

来避免使用-exec
find . -type f -name 'Makefile' -exec egrep -Hn "pattern" {} \;
-H上的{p> egrep输出匹配文件的完整路径。

答案 4 :(得分:1)

grep -R" string" /路径

请找到此链接 http://rulariteducation.blogspot.in/2016/03/how-to-check-particluar-string-in-linux.html

答案 5 :(得分:0)

你可以使用ff命令,即ff -p .format。例如ff -p * .txt

答案 6 :(得分:0)

查找占用大磁盘空间的大文件

我们需要结合多个命令。

find . -type f | xargs du -sk | sort -n | tail;