Próbowałem znaleźć sposób, aby to zrobić z find
, ale wydaje się, że nie ma czegoś takiego jak opcja -breadth
. Krótki pisać poprawkę na to, wypróbuj następujące powłoki zaklęcie (dla bash):
LIST="$(find . -mindepth 1 -maxdepth 1 -type d)";
while test -n "$LIST"; do
for F in $LIST; do
echo $F;
test -d "$F" && NLIST="$NLIST $(find $F -maxdepth 1 -mindepth 1 -type d)";
done;
LIST=$NLIST;
NLIST="";
done
ja rodzaj natknął się to przypadkowo, więc nie wiem, czy to działa w ogóle (ja testowałem go tylko na specyficzna struktura katalogów pytałeś o)
Jeśli chcesz ograniczyć głębokość, umieścić zmienną licznika w zewnętrznej pętli, tak jak (mam także dodawanie komentarzy do tego):
# initialize the list of subdirectories being processed
LIST="$(find . -mindepth 1 -maxdepth 1 -type d)";
# initialize the depth counter to 0
let i=0;
# as long as there are more subdirectories to process and we haven't hit the max depth
while test "$i" -lt 2 -a -n "$LIST"; do
# increment the depth counter
let i++;
# for each subdirectory in the current list
for F in $LIST; do
# print it
echo $F;
# double-check that it is indeed a directory, and if so
# append its contents to the list for the next level
test -d "$F" && NLIST="$NLIST $(find $F -maxdepth 1 -mindepth 1 -type d)";
done;
# set the current list equal to the next level's list
LIST=$NLIST;
# clear the next level's list
NLIST="";
done
(zamień 2 w -lt 2
na głębokość)
Zasadniczo implementuje standardowy algorytm przeszukiwania według szerokości, używając $LIST
i jako kolejki nazw katalogów.Oto ostatnie podejście jako jedną wkładką do łatwego kopiowania i wklejania:
LIST="$(find . -mindepth 1 -maxdepth 1 -type d)"; let i=0; while test "$i" -lt 2 -a -n "$LIST"; do let i++; for F in $LIST; do echo $F; test -d "$F" && NLIST="$NLIST $(find $F -maxdepth 1 -mindepth 1 -type d)"; done; LIST=$NLIST; NLIST=""; done
mógłbyś rozwinąć ten obejmuje język z wyboru, a OS –
Arg (Linux?)! To jest pytanie "wiki społeczności". Denerwujący. –
co sprawia, że jest to pytanie typu wiki społeczności? –