可实现在不同目录下的递归查找文件内容
源码:
#!/bin/sh
if [ -n "$2" ];then
path=$2
else
path=`pwd`
fi
cd $path
for i in `ls`
do
log=$path/$i
if [ -d "$log" ];then
cd $log
for j in `ls`
do
if [ -f "$j" ];then
rs=`cat $j|grep $1`
if [ -n "$rs" ];then
echo ==== in `pwd`/$j =====
cat $j|grep $1
echo ==== end =====
echo
fi
else
npath=`pwd`
hpfinder $1 $npath
fi
done
else
rs=`cat $log|grep $1`
if [ -n "$rs" ];then
echo ==== in $log =====
cat $log|grep $1
echo ==== end =====
echo
fi
fi
done
./hpfinder 查找的内容 [查找目录]
[root@EM-FGXTT2X test]# pwd
/tmp/test
[root@EM-FGXTT2X test]# tree .
.
|-- a
|-- b
|-- c
`-- dir2
|-- a
|-- b
`-- c
1 directory, 6 files
在目录/tmp/test 下的文件a b c,和子目录dir2下的a b c 内容下
[root@EM-FGXTT2X test]# cat a b c
one line1
two line2
one line1
two line2
aaaaaaaaa
bbbbbbbbb
line3
执行
hpfinder line
结果:
[root@EM-FGXTT2X test]# hpfinder line
==== in /tmp/test/a =====
one line1
two line2
==== end =====
==== in /tmp/test/b =====
one line1
two line2
==== end =====
==== in /tmp/test/c =====
line3
==== end =====
==== in /tmp/test/dir2/a =====
one line1
two line2
==== end =====
==== in /tmp/test/dir2/b =====
one line1
two line2
==== end =====
==== in /tmp/test/dir2/c =====
line3
==== end =====