本文共 690 字,大约阅读时间需要 2 分钟。
干了一件傻事,全文遍历查找一个字符串,将经过写下来
find / -type f -fstype ext4 -exec sh -c \'file="{}";type=$(file $file);[[ $type =~ " text" ]] && echo $file' \; \|xargs grep abcdef
思路
1、用find在一定范围找出所有文件
2、在find子命令中执行file命令判断文件类型,过滤出文本文件
3、在文本中过滤出字符串
为了提高性能,加入-user 和-mtime 作为限制条件进行过滤
-user 加运行程序的用户
全命令
find / -type f -fstype ext4 -user root -exec sh -c \'file="{}";type=$(file $file);[[ $type =~ " text" ]] && echo $file' \; \|xargs grep abcdef
-mtime 修改时间, -60 代表两个月内的新文件,
为了提高grep的效率,这里采用fgrep,fgrep不支持正则,干的活比grep更少,所有更快。
为了避免匹配结果过多造成刷屏,给grep加上参数-l,只显示匹配的文件名。
全命令
find / -type f -fstype ext4 -mtime -60 -exec sh -c \'file="{}";type=$(file $file);[[ $type =~ " text" ]] && echo $file' \; \|xargs fgrep -l abcdef
转载地址:http://xjvpl.baihongyu.com/