遍历指定目录下的文件名
#!/bin/bash
traverse(){
folder="/home/shipan/Work/MnistVariant/"
for file in ${folder}*
do
filename=$(basename $file) # file是包含路径的文件名全称,使用basename $file可以提取出文件名
if [[ "${filename}" == lenet_train_test*.prototxt ]] # 模糊匹配时需要使用[[]],此处模糊匹配文件名以lenet_train_test开始以.prototxt结尾的文件
then echo $filename
fi
done
}
traverse
Tips
使用
dirname $file
可以提取出目录.
如果运行shell脚本时报错[ :Unexpected operator
或者[[: Permission denied
请使用bash /path/script.sh
,因为默认使用sh命令来运行脚本,但是sh中语句需要;.