#!/bin/bash
[ $# -gt 2 ] && {
echo "Usage: ${0##*/} <dir=.> <deep=1>"
exit 1
}
TOP=0
QUEUELIST[${#QUEUELIST[@]}]=`cd ${1:-.};pwd`
DEEPMAX=${2:-1}
DEEP=0
#The number of directories in the upper directory(DEEP-1)
X=1
#The number of directories in the next directory(DEEP+1)
Y=0
#The number of directories scanned in the current directory(DEEP)
Z=0
while [ $TOP -ne ${#QUEUELIST[@]} ]
do
curdir=${QUEUELIST[$TOP]}
cd $curdir
((TOP++))
for list in `ls`
do
if [ -d $list ]
then
echo $curdir/$list
QUEUELIST[${#QUEUELIST[@]}]=$curdir/$list
((Y++))
fi
done
((Z++))
if [ $Z -eq $X ];then
((DEEP++))
if [ $DEEP -eq $DEEPMAX ];then
exit 0
fi
X=$Y
Y=0
Z=0
fi
done
说明: 其实用find就可以,只要指定-maxdepth即可,这里只为练练手