He intentado encontrar una manera de hacer esto con find
pero no parecen tener nada como una opción -breadth
. Corto de escribir un parche para ello, pruebe el siguiente conjuro de comandos (por 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
en cierto modo me topamos con este accidente, así que no sé si funciona en general (que estaba probando que sólo en la estructura de directorios específica que estabas preguntando acerca)
Si desea limitar la profundidad, poner una variable de contador en el bucle exterior, al igual que (también estoy añadiendo comentarios a éste):
# 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
(reemplace el 2 en -lt 2
con la profundidad)
Básicamente esto implementa el algoritmo de búsqueda de amplitud estándar usando $LIST
y $NLIST
como una cola de nombres de directorio.Aquí está el último enfoque como una sola línea para una fácil de copiar y pegar:
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
Podría extenderse sobre este para incluir el idioma de su elección, y el sistema operativo –
Arg (Linux?)! Esta es una pregunta de "comunidad wiki". Molesto. –
¿Qué hace que esta sea una pregunta wiki de la comunidad? –