首先,创建一个关键词列表文件,例如:keyworks.list。每行指定一个要查找的关键词。
接着,创建一个 shell 脚本,内容如下:
#!/bin/bash
# 作者:靑龍一笑(C.S.Ricen)
# 功能:根据指定的关键词,查找指定目录下的文件内容
# 要查找的目录
Search_Dir=/opt/datas/
# 关键字列表
Keyworks_List=keyworks.list
if [ ! -f $Keyworks_List ]; then
echo "请先设置关键词列表!"
exit 1
fi
if [ ! -d $Search_Dir ]; then
echo "要查找的目录不存在!"
exit 1
fi
# 整理关键词列表
while read -r line
do
if [ -z $list ] && [ -n $line ]; then
list=$list$line
elif [ ! -z $line ] ; then
list=$list"|"$line
fi
done < $Keyworks_List
# 查找
find $Search_Dir | xargs grep -riEo $list > result.txt
# 结果去重
awk '!a[$0]++' result.txt > result.tmp && mv -f result.tmp result.txt
exit 0
给这个 shell 脚本赋予可执行权限并执行。在当前目录下会生成一个 result.txt 文件,里面记录着查询的结果信息。
Copyright © 2005-2023 by www.ricensoftwares.com.cn All Rights Reserved.