Questo è ancora un ciclo, in quanto utilizza il comando filiale di sed
:
find -depth -type d |sed 'h; :b; $b; N; /^\(.*\)\/.*\n\1$/ { g; bb }; $ {x; b}; P; D'
Sulla base di uno script in info sed
(uniq lavoro-alike).
Edit Ecco lo script sed
scoppiata con i commenti (copiati da info sed
e modificato):
# copy the pattern space to the hold space
h
# label for branch (goto) command
:b
# on the last line ($) goto the end of
# the script (b with no label), print and exit
$b
# append the next line to the pattern space (it now contains line1\nline2
N
# if the pattern space matches line1 with the last slash and whatever comes after
# it followed by a newline followed by a copy of the part before the last slash
# in other words line2 is different from line one with the last dir removed
# see below for the regex
/^\(.*\)\/.*\n\1$/ {
# Undo the effect of
# the n command by copying the hold space back to the pattern space
g
# branch to label b (so now line2 is playing the role of line1
bb
}
# If the `N' command had added the last line, print and exit
# (if this is the last line then swap the hold space and pattern space
# and goto the end (b without a label)
$ { x; b }
# The lines are different; print the first and go
# back working on the second.
# print up to the first newline of the pattern space
P
# delete up to the first newline in the pattern space, the remainder, if any,
# will become line1, go to the top of the loop
D
Ecco cosa la regex sta facendo:
/
- avviare un modello
^
- corrisponde all'inizio della riga
\(
- avviare un gruppo di cattura (subexpression di riferimento posteriore)
.*
- zero o più (*) di qualsiasi carattere (.)
\)
- end gruppo cattura
\/
- una barra (/) (fuggito con \
)
.*
- zero o più di qualsiasi carattere
\n
- un ritorno a capo
\1
- una copia di il riferimento posteriore (che in questo caso è qualsiasi cosa era tra l'inizio della riga e l'ultima barra)
$
- corrisponde alla fine della riga
/
- porre fine al modello
fonte
2009-10-15 20:31:16
Con il vostro esempio, 'trovare mydir -mindepth 2 - il tipo d' funzionerebbe, ma non lo farà quando si hanno più profondità massime. Vuoi davvero elencare solo le directory che non contengono altre directory o vuoi vedere un particolare livello di struttura delle directory? – Cascabel
Sì, grazie per il chiarimento - il mio esempio è stato un esempio semplicistico. Stavo davvero cercando una soluzione più generale. Inoltre, sto cercando di vedere la struttura generale delle directory, non mi interessa davvero dei file nelle directory (quindi anche leaf significa "leaf directory" in questo contesto). Grazie. – amol