[shell]Linux在指定目录下查找包含指定字符串的文件
#!/bin/bash
echo -e "\nThis is a program to find the file in special directorywhich include keyword!\n\n"
if [ "$1" == "" ]; then
fi
test ! -d $dir && echo -e "The $diris not exist in your system\.\n\n"&& exit 0
count=0
filelist=`ls -R $dir 2> /dev/null | grep -v'^$'`
for filename in $filelist
do
?? ?temp=`echo$filename | sed 's/:.*$//g'`
?? ?if ["$filename" != "$temp" ]; then
??????curdir=$temp
??????#echo "current dir = $curdir"
?? ?else
??????filetype=`file $curdir/$filename | grep"text"`
??????if [ "$filetype" != "" ]; then
?????????temp=`grep $keyword $curdir/$filename2> /dev/null`
?????????#echo $curdir/$filename
?????????if [ "$temp" != "" ]; then
????????????echo $curdir/$filename
????????????count=$(($count+1))
?????????fi
??????fi
?? ?fi
done
echo -e "\n\nTotal: $count"
echo -e "Done"
文件保存为myfind.sh
执行:chmod u+x myfind.sh
例如:
myfind.sh cs /home/zk
在/home/zk目录下查找包含cs的字符
可以达到同样的目的,不是吗