第一种方法:
#!/bin/bash
echo -e "\nThis is a program to find the file in special directory which include keyword!\n\n"
if [ "$1" == "" ]; then
echo -e "Please input the keyword behind the $0\.\n\n"
exit 0
fi
keyword=$1
dir=.
if [ "$2" != "" ]; then
dir=$2
fi
test ! -d $dir && echo -e "The $dir is 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/$filename 2> /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的字符
echo -e "\nThis is a program to find the file in special directory which include keyword!\n\n"
if [ "$1" == "" ]; then
fi
keyword=$1
dir=.
if [ "$2" != "" ]; then
fi
test ! -d $dir && echo -e "The $dir is 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
done
echo -e "\n\nTotal: $count"
echo -e "Done"
文件保存为myfind.sh
执行:chmod u+x myfind.sh
例如:
myfind.sh cs /home/zk
在/home/zk目录下查找包含cs的字符
第二种方法:
文件的查找
1. which (寻找『执行文件』)
# which [-a] command 参数: -a :列出所有可以找到的
2. whereis (寻找 【特定文件】 )
# whereis [-bmsu] 文件或目录名参数: -b :只找 binary 的文件 -m :只找在说明文件, manual 路径下的文件 -s :只找 source 来源文件 -u :没有说明文件的文件
3.locate <文件名称>
在文件索引数据库中搜索文件
4. find [路径] <表达式>
5.grep
目录中查询豆豆我最常用就是:grep -rm '查询字符串' /var/www/doudou
第三种方法: