export arrayindex export array function scandir() { local cur_dir parent_dir workdir workdir=$1 cd ${workdir} if [ ${workdir} = "/" ] then cur_dir="" else cur_dir=$(pwd) fi for dirlist in $(ls ${cur_dir}) do if test -d ${dirlist};then cd ${dirlist} scandir ${cur_dir}/${dirlist} cd .. else # echo ${cur_dir}/${dirlist} #数组赋值 array[$arrayindex]=${cur_dir}/${dirlist} #注意值递增的方式 ((arrayindex++)) fi done } function GetALLDirInfo() { local dir dir=$1 arrayindex=0 if test -d $dir then scandir $dir elif test -f $dir then echo "you input a file not a directory" exit 1 else echo "the dir you input $dir is not exit" fi } read -p "please input the dir path:" DIR echo "path is:$DIR" GetALLDirInfo $DIR for content in ${array[@]} do #注意这里一定是ls命令,还有后面的通过管道来调用CP命令 ls $content|grep 'txt'|awk '{print $0}' |xargs -I {} cp {} /home/song/temp1 done