G0dspeed's Blog

Stay Hungary, Stay Foolish
  1. 首页
  2. 经验技巧
  3. 正文

Linux下遍历查找某的文件包含某个关键字的命令

2016/02/29 1891点热度 0人点赞 0条评论

如下命令

grep -rnw '/path/to/somewhere/' -e "pattern"

-r 或者 -R 为递归查找, -n 是行号 -w 为要求全匹配. -l (小写L) 参数可以只列出文件名.

除此以外, --exclude 或者 --include 参数可以提供更有效的查找。比如:

grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"

这个可以查找扩展名为.c或者.h的文件. --exclude的用法也类似:

grep --exclude=*.o -rnw '/path/to/somewhere/' -e "pattern"

上面这行命令在搜索中会排除扩展名为.0的文件。和包含/不包含文件一样,目录也可以使用 --exclude-dir 和 --include-dir 参数。 比如下面列出怎么使用 --exclude-dir:

grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"

如果单是遍历查找文件的话可以用

find . -name '*.xml'

如果想遍历删除某种类型的文件的话,可以用

 find . -name '*.xml' -type f -delete
标签: Linux
最后更新:2016/03/01

G0dspeed

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

取消回复

Time limit is exhausted. Please reload CAPTCHA.

COPYRIGHT © 2022 G0dspeed's Blog. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang