参考:lunar1983的专栏
实现:从给定目录树中grep出含制定字符串的行,并给出所在路径
代码如下所示:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 |
#!/bin/sh -if
[ $# -ne 2 ] then
echo "usage traverse <dir> <str>" exitfi{function
ergodic(){for file in ` ls $1`do if
[ -d $1"/"$file
] #如果 file存在且是一个目录则为真 then ergodic $1"/"$file
$2 else local
path=$1"/"$file
#得到文件的完整的目录 local
name=$file
#得到文件的名字 if
[[ `cat
$path | grep
$2` ]] then
echo "" echo
LOC:$path fi cat
–n $path | grep
$2 fidone}}ergodic $1 $2 |
效果图:
Linux_shell脚本_遍历文件夹下所有文件,布布扣,bubuko.com
原文:http://www.cnblogs.com/shenerguang/p/3762802.html