详解shell 遍历文件夹内所有文件并打印绝对路径

例如你有一个文件夹路径是 /wls,如果想要遍历这个文件夹内的所有文件,并将它们保存到数组中,利用shell你可以这样做

for file in /wls/*
do
    if test -f $file
    then
        arr=(${arr[*]} $file)
    fi
done
echo ${arr[@]}

这样就可以遍历wls文件夹下的所有文件并把它们的绝对路径存在arr这个数组中,但这还是不够的,因为文件夹中可能会有多个子文件夹,如果只过滤文件会遗漏掉子文件夹中的文件,所以需要扩展上面的方法,如果我们只考虑两级目录的话可以这样写

for file in /wls/*
do
    if test -f $file
    then
        arr=(${arr[*]} $file)
    else
        for subfile in $file
            do
                if test -f $subfile
                then
                    arr=(${arr[*]} $subfile)
                fi
            done
    fi
done
echo ${arr[@]}

这样就可以把目标文件夹下的两级目录内所有文件的绝对路径获取到,那么如果有多级子目录的情况呢,这就要求我们去写一个function来协助我们

function getdir(){
    echo $1
    for file in $1/*
    do
    if test -f $file
    then
        echo $file
        arr=(${arr[*]} $file)
    else
        getdir $file
    fi
    done
}
getdir /wls
echo ${arr[@]}

通过递归调用getdir就可以实现将wls文件夹内所有文件的绝对路径保存到数组中。

以上的方法在处理NAS日志的问题中很有效,可以将所有日志文件遍历一遍,当然关于日志文件名的过滤这里要读者自己实现。