您如何通过“查找"获得grep结果? [英] How do you grep results from 'find'?

查看:117
本文介绍了您如何通过“查找"获得grep结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图查找在find命令的结果文件名中包含的单词/模式.

Trying to find a word/pattern contained within the resulting file names of the find command.

例如,我有以下命令:

find . -name Gruntfile.js返回几个文件名.

我该如何在其中重复输入单词模式?

How do I grep within these for a word pattern?

正在考虑以下方面的问题:

Was thinking something along the lines of:

find . -name Gruntfile.js | grep -rnw -e 'purifycss'

但是,这是行不通的.

推荐答案

使用-exec {} +选项将文件名列表传递给grep:

Use the -exec {} + option to pass the list of filenames that are found as arguments to grep:

find -name Gruntfile.js -exec grep -nw 'purifycss' {} +

这是最安全,最有效的方法,因为当文件的路径不是行为良好"时,它不会中断. (例如,包含一个空格).就像使用xargs的方法一样,它还通过一次传递多个文件名来最大程度地减少了对grep的调用次数.

This is the safest and most efficient approach, as it doesn't break when the path to the file isn't "well-behaved" (e.g. contains a space). Like an approach using xargs, it also minimises the number of calls to grep by passing multiple filenames at once.

我已经删除了-e-r开关,因为我认为它们对您没有用.

I have removed the -e and -r switches, as I don't think that they're useful to you here.

man find的摘录:

-exec command {} +
-exec操作的此变体在选定的文件上运行指定的命令,但是通过在末尾附加每个选定的文件名来构建命令行.该命令的调用总数将大大少于匹配文件的数目.

-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 invocations of the command will be much less than the number of matched files.

这篇关于您如何通过“查找"获得grep结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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