ho cercato di trovare un modo per fare questo con find
ma non sembra avere nulla come una scelta -breadth
. Breve di scrivere una patch per esso, provare quanto segue shell formula magica (per 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
I sorta di accidentalmente imbattuto in questo modo non so se funziona in generale (stavo testando solo su la struttura specifica directory che stavi chiedendo circa)
Se si vuole limitare la profondità, mettere una variabile contatore nel ciclo esterno, in questo modo (sto anche l'aggiunta di commenti a questo):
# 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
(sostituire il 2 in -lt 2
con la profondità)
Fondamentalmente questo implementa l'algoritmo di ricerca standard in larghezza utilizzando $LIST
e $NLIST
come coda di nomi di directory.Ecco il secondo approccio come un one-liner per un facile copia-e-incolla:
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
fonte
2009-02-12 01:26:59
Potrebbe estendere a questo per includere lingua di propria scelta, e il sistema operativo –
Arg (Linux?)! Questa è una domanda "wiki della comunità". Fastidioso. –
cosa rende questa una domanda wiki della comunità? –