平时工作中,有时候忘记了所储存的信息的具体位置,一个文件一个文件的查找比较耗费时间,那么如果我们有一个shell的脚本,输入需要查找的目录和关键字,就可以快速定位到具体文件的具体行数,如下
#!/bin/bash
# set -e
function read_dir() {
for file in `ls $1`
do
if [ -d $1"/"$file ]; then
read_dir $1"/"$file $2
else
result=`grep -n $2 $1"/"$file`
# echo ${#result}
if [ ${#result} -ge 1 ]; then
echo $1"/"$file
echo $result
echo
fi
fi
done
}
read -p "Please input the directory you will check:" checkDir
echo
if [ ! -d $checkDir ]; then
echo "$checkDir is not a directory, exit now"
exit 1
fi
read -p "Please input the string you want to locate:" str
echo
if [ $str == "" ]; then
echo "your input is null"
exit 1
fi
read_dir $checkDir $str