Shell 脚本递归列表目录下所有文件

#!/bin/bash

if [[ -z "$1" ]] || [[ ! -d "$1" ]]; then
    echo "The directory is empty or not exist!"
    echo "It will use the current directory."
    nowdir=$(pwd)
else
    nowdir=$(cd $1; pwd)
fi
echo "$nowdir"

# 递归函数的实现
function SearchCfile()
{
    cd $1
    #这里可以修改为判断文件类型,如.c,.java等等文件类型,修改一下grep条件就可以了
    #cfilelist=$(ls -l | grep "^-" | awk '{print $9}')
    cfilelist=$(ls -l | awk '{print $9}')
    pwd=$1

    for cfilename in $cfilelist
    do
        if [[ -d "$cfilename" ]];then
            SearchCfile $cfilename
            cd ..
        else
            #echo $cfilename
            ext=${cfilename#*.}
            if [[ "$ext" == "mp4" ]];then
                pwd=$(pwd)
                echo ${pwd}/${cfilename}
            fi;
        fi;
    done;

}

SearchCfile $nowdir
评论

您还没有登陆,登陆后可以发表评论哦!点击登陆