比如在当前路径下有个test文件夹,其中包含 a、b、 c、d.c 四个文件,内容如下:
[root@rhel6www ~]# pwd
/root
[root@rhel6www ~]# ll -d test/
drwxr-xr-x 2 root root 4096 Apr 11 02:15 test/
[root@rhel6www ~]#
[root@rhel6www test]# cat a
venus
[root@rhel6www test]# cat b
venusnq
[root@rhel6www test]# cat c
saslkdjfalsdkjflk
[root@rhel6www test]# cat d.c
hahahvenus
现在需要寻找包含“venus”字符串的文件。
[root@rhel6www ~]# find test/ -type f | xargs grep “venus”
test/a:venus
test/b:venustsui
test/d.c:hahahvenus
find + 搜索目录 -type f 只寻找文件。
这样是搜索所有文件,在linux下,软件源码包中有.h 、.c文件,如果我们从*.c 中搜索某个字符串。可以这样。
[root@rhel6www ~]# find test/ -type f -name “*.c” | xargs grep “venus”
hahahvenus
这样排除了部分文件,提高效率。
专注于 服务器运维与web架构
E-mail:venus#rootop.org