我有一个如下的目录结构:
.
├── Untitled Folder
│ ├── Untitled Folder
│ │ └── zing
│ │ ├── first
| | | └── dum
│ │ ├── second
│ │ └── third
│ ├── Untitled Folder 2
│ └── Untitled Folder 3
├── Untitled Folder 2
│ ├── Untitled Folder
│ ├── Untitled Folder 2
│ │ └── zing
│ │ ├── fifth
| | | └── dum
│ │ ├── fourth
│ │ └── sixth
│ └── Untitled Folder 3
└── Untitled Folder 3
├── Untitled Folder
├── Untitled Folder 2
└── Untitled Folder 3
└── zing
├── eighth
└── seventh
└── dum
我只想列出所有zing
目录的直接子目录。
示例输出为:
first
second
third
fifth
fourth
sixth
eighth
seventh
我试过
% find . -type d | grep zing
./Untitled Folder 3/Untitled Folder 3/zing
./Untitled Folder 3/Untitled Folder 3/zing/eigthth
./Untitled Folder 3/Untitled Folder 3/zing/seventh
./Untitled Folder 3/Untitled Folder 3/zing/seventh/dum
./Untitled Folder 2/Untitled Folder 2/zing
./Untitled Folder 2/Untitled Folder 2/zing/fifth
./Untitled Folder 2/Untitled Folder 2/zing/fifth/dum
./Untitled Folder 2/Untitled Folder 2/zing/fourth
./Untitled Folder 2/Untitled Folder 2/zing/sixth
./Untitled Folder/Untitled Folder/zing
./Untitled Folder/Untitled Folder/zing/first
./Untitled Folder/Untitled Folder/zing/first/dum
./Untitled Folder/Untitled Folder/zing/third
./Untitled Folder/Untitled Folder/zing/second
zing
它甚至显示了(在本例中)的孩子的孩子dum
,这是我不想要的。
我怎样才能得到我的预期输出?
答案1
Bash、Globstar 已激活(使用
shopt -s globstar
)。printf '%s\n' **/zing/*/
仅对于最后一个路径组件,
for dir in **/zing/*/; do basename "$dir"; done
POSIX 查找。
find . -type d -path '*/zing/*' -prune
-prune
避免在匹配的目录中降序。仅对于最后一个路径组件,
find . -type d -path '*/zing/*' -prune -exec basename {} \;
答案2
尝试这个:
find . -type d -regex ".*/zing/[^/]+"
答案3
你可以找到所有津目录
find . -type d -name zing
如果需要获取目录的内容,请在每个目录中执行“ls”。
或者可以迭代 find( find \{\} -type d -maxdepth 1 -mindepth 1
) 以仅获取直接子目录名称。然后你可以使用basename
只获取目录名称,或者一些丑陋的黑客,如| rev | cut -f1 -d/ | rev
.