如何在具有特定扩展名的文件中递归grep? [英] How do I grep recursively in files with a certain extension?

查看:66
本文介绍了如何在具有特定扩展名的文件中递归grep?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在子目录中查找所有扩展名为 .out 的文件路径,我使用

To find all file paths with .out extension in subdirectories, I use

find . -name '*.out'

要在所有以 .out 结尾的文件中复制模式,我使用

To grep a pattern in all files ending in .out, I use

grep pattern *.out

如何结合这两个命令,以便找到所有文件,然后抓取这些文件?

How do I combine these two commands, so that it finds all files and then greps in those files?

我正在寻找一种优雅的替代品

I am looking for an elegant alternative to

grep -r 'pattern' . | grep '.out'

推荐答案

查找允许您使用 -exec 选项在找到的每个文件上运行程序:/p>

find allows you to run a program on each file it finds using the -exec option:

find -name '*.out' -exec grep -H pattern {} \;

{} 表示文件名,然后; 告诉 find 这是 grep . -H 告诉 grep 始终打印文件名,通常仅在有多个文件要处理时才会这样做.

{} indicates the file name, and ; tells find that that's the end of the arguments to grep. -H tells grep to always print the file name, which it normally does only when there are multiple files to process.

这篇关于如何在具有特定扩展名的文件中递归grep?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆