这仍然是一个循环,因为它在sed
使用分支命令:
find -depth -type d |sed 'h; :b; $b; N; /^\(.*\)\/.*\n\1$/ { g; bb }; $ {x; b}; P; D'
基于在info sed
(uniq的工作一样)的脚本。
编辑这里是sed
脚本注释爆发(从info sed
复制和修改):
# 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
这里是正则表达式是这样做的:
/
- 启动模式
^
- 匹配行首
\(
- 启动捕获组(返回参考子表达式)
.*
- 零个或多个(*)任意字符(。)
\)
- 端捕获组
\/
- 一斜杠(/)(其中\
转义)
.*
- 零个或多个任意字符
\n
- 一个新行
\1
- 副本(在这种情况下,无论是在行的开头和最后一个斜杠之间)
$
- 匹配行尾
/
- 结束模式
你的榜样,'找到mydir的-mindepth 2 - 类型d'可以工作,但是当你有多个最大深度时它不会。你是否真的只想列出不包含其他目录的目录,或者你是否希望看到特定级别的目录结构? – Cascabel 2009-10-15 19:17:54
是的,谢谢澄清 - 我的例子是一个简单的例子。我的确在寻求更为一般的解决方案。 此外,我期待看到一般的目录结构,并不真正关心目录中的文件(所以叶也就意味着在这个上下文中的“叶目录”)。谢谢。 – amol 2009-10-15 21:09:38