shell 脚本遍历指定文件夹下的所有文件

it2022-05-05  164

#!/bin/bash function readDir(){ #echo $1 #echo $2 targetDir=`ls $1` # 是键盘上'~'下面的符号,不是单引号 for fileName in $targetDir do if [ -d $1"/"$fileName ] # 两端要有空格,不然报错 then #echo $1"/"$fileName readDir $1"/"$fileName $2 else #echo $fileName #echo ${fileName:0-3} if [[ ${fileName:0-3} == '.py' ]]; # 查找扩展名为 .py的文件 then echo $1"/"$fileName >> $2/findPy elif [[ ${fileName:0-5} == '.html' ]]; # 查找扩展名为 .html的文件 then echo $1"/"$fileName >> $2/findHtml elif [[ ${fileName:0-3} == '.js' ]]; # 查找扩展名为 .js的文件 then echo $1"/"$fileName >> $2/findJs elif [[ ${fileName:0-2} == '.h' ]]; # 查找扩展名为 .h的文件 then echo $1"/"$fileName >> $2/findC fi fi done } readDir $1 $2 #分别代表两个参数,即后面调用时传入的 '遍历目标文件夹路径' 和 '遍历结果保存路径'

给脚本添加执行权限:

chmod 755 findClass.sh (7:x:执行=1 + r:读=4 +w:写=2;5:x + r) 或者 chmod +x findClass.sh

执行脚本:

./findClass.sh '遍历目标文件夹路径' '遍历结果保存路径'

最新回复(0)