前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用shell搜索匹配文件

使用shell搜索匹配文件

作者头像
码农二狗
发布2018-06-29 10:15:30
1.1K0
发布2018-06-29 10:15:30
举报
文章被收录于专栏:码农二狗码农二狗

前两天面tx都有问到这个问题,我知道大致能通过find和grep实现,但是具体没写过.刚刚无事翻了下命令手册,发现find的一个动作:

代码语言:javascript
复制
ACTIONS
   -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.  Both of these constructions might need to be escaped (with a `\') or quoted to protect them from expansion by the shell.  See the EXAMPLES section for examples of the use of the -exec option.  The specified command is  run  once for  each  matched  file.   The command is executed in the starting directory.   There are unavoidable security problems surrounding use of the -exec action; you should use the -execdir option instead.

从描述来看就是执行命令,其数据来源于find的结果,如果命令返回0(即成功)则输出.

结合grep搜索:

代码语言:javascript
复制
find ./ -name "*.php" -exec grep "system" {} \;

执行,返回的是文件内容,需要返回文件名则需要对grep命令使用-l参数:

代码语言:javascript
复制
find ./ -name "*.php" -exec grep -l "system" {} \;
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档