shell遍历目录下指定后缀名的文件

时间:2021-01-13 08:47:16

先把代码放在这里,有时间我再整理,不懂的可以问我

#!/bin/sh
#su root

#script name:filename.sh
sourcedir=$1
cd $sourcedir

list_alldir(){
    for file2 in `ls -a $1`
    do
       if [ x"$file2" != x"." -a x"$file2" != x".." ];then
            if [ -d "$1/$file2" ];then
                echo "$1/$file2"
                list_alldir "$1/$file2"
            fi
        fi
    done
}
names=$(list_alldir $sourcedir)
echo " " > names.txt
for j in ${names[*]} $1
do
        echo $j

        ls -l $j | awk '/^-.*/{print tmpname1$9}' tmpname1="$tmpname" | sed '/^ *$/d ' >> names.txt
done

totals=$(cat names.txt | wc -l)
array=$(awk '/.*\.[ch]$/{print $1}
        /.*\.cc$/{print $1;}
        /.*\.cpp$/{print $1;}
        /.*\.hh$/{print $1;}
' names.txt)
for j in ${array[*]}
do
        echo $j
done