He encontrado esta pregunta afte r Ya había descubierto mi propio script similar. Parece ajustarse a tus condiciones y es muy flexible, así que pensé en agregarlo como respuesta.
Ventajas:
- se pueden agrupar a cualquier profundidad (0 para
.
, 1 para los subdirectorios de primer nivel, etc.)
- salida se imprime bastante
- ningún bucle, y sólo una
find
de mando, así que es un poco más rápido en grandes directorios
- todavía puede ser sintonizado para añadir filtros personalizados (maxdepth para que sea no recursivo, el nombre del archivo de patrones)
código prima:
find -P . -type f | rev | cut -d/ -f2- | rev | \
cut -d/ -f1-2 | cut -d/ -f2- | sort | uniq -c
Envuelto en una función y explicó:
fc() {
# Usage: fc [depth >= 0, default 1]
# 1. List all files, not following symlinks.
# (Add filters like -maxdepth 1 or -iname='*.jpg' here.)
# 2. Cut off filenames in bulk. Reverse and chop to the
# first/(remove filename). Reverse back.
# 3. Cut everything after the specified depth, so that each line
# contains only the relevant directory path
# 4. Cut off the preceeding '.' unless that's all there is.
# 5. Sort and group to unique lines with count.
find -P . -type f \
| rev | cut -d/ -f2- | rev \
| cut -d/ -f1-$((${1:-1}+1)) \
| cut -d/ -f2- \
| sort | uniq -c
}
produce una salida como esta:
$ fc 0
1668 .
$ fC# depth of 1 is default
6 .
3 .ssh
11 Desktop
44 Downloads
1054 Music
550 Pictures
Por supuesto, con el primer número que se puede canalizar a sort
:
$ fc | sort
3 .ssh
6 .
11 Desktop
44 Downloads
550 Pictures
1054 Music
Para la persona que votó cerca de: Bash es un lenguaje real . – Bernard