shell 遍历目录 批量解压文件名含有某字符串及特定后缀的文件
更新:HHH   时间:2023-1-7


#!/bin/sh
mkdir  /home/zhengyk/Desktop/tmp  #创建临时文件夹,用来存放解压后的文件
#mkdir  /home/zhengyk/Desktop/local  #创建临时文件夹,用来存放过滤结果

list_alldir(){   
	for file in $1/*
do
if [ -d $file ]; then
	list_alldir $file
else
	filename=${file##*/}  # 从路径中取出文件名及后缀
echo "$filename"
if [[ `echo $filename | awk -F'.' '$0~/.*123.*zip/{print $3}'` = "zip" ]]    # "123"为文件名里的子串
#elif [[ `echo $file | awk -F'.' '$0~/.*123.*zip/{print ${file##*.}}'` = "zip" ]]
then
#tar jxf $file -C /home/zhengyk/Desktop/tmp   #解压文件ddd
unzip -o  $file -d /home/zhengyk/Desktop/tmp   #解压文件
#grep_word /home/zhengyk/Desktop/tmp   #执行过滤关键字的函数
#rm -rf  /home/zhengyk/Desktop/tmp/*     #清理现场,为解压下一个文件做准备!
	echo "$file ..........ok!"   #显示被处理的bz2文件!
fi
fi
done
}

grep_word(){
for fileb in $1/*
do
if [ -f $fileb ]; then
# “bkeep”是你想要过滤的关键字,根据实际,自行设定
grep -H "t" $fileb >> /home/zhengyk/Desktop/tmp.txt   #H 结果中显示文件名,方便我们阅读!
else
	grep_word $fileb
fi
done
}

if [ $# -gt 0 ]
then
list_alldir "$1"
else
echo "please input:./list_alldir.sh dirpath"
fi



参考链接:http://www.360doc.com/content/10/0928/16/3234041_57087955.shtml


返回开发技术教程...