0

I have a directory structure like the following:

.
├── 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

I want to list only direct child directories of all zing directories.

Sample output is:

first
second
third
fifth
fourth
sixth
eighth
seventh

I tried

% 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

It is even showing the child of the child of zing (in this case dum), which I do not want.

How can I get my expected output?

3 Answers 3

5
  • Bash, Globstar activated (with shopt -s globstar).

    printf '%s\n' **/zing/*/
    
    • For only the last path component,

       for dir in **/zing/*/; do basename "$dir"; done
      
  • POSIX Find.

    find . -type d -path '*/zing/*' -prune
    

    -prune avoids descending in the matched directories.

    • For only the last path component,

      find . -type d -path '*/zing/*' -prune -exec basename {} \;
      
3

Try this:

find . -type d -regex ".*/zing/[^/]+"
2
  • 2
    Very nice use of the GNU's -regex flag! The asker can add -exec basename {} \; to remove the path components of the output if that is indeed a must.
    – Quasímodo
    Feb 6, 2021 at 0:12
  • Or -printf '%f\n' Feb 8, 2021 at 18:05
0

You could find all zing directories

find . -type d -name zing

and execute a "ls" in each if you need to get the directory's contents.

Or maybe iterate the find (find \{\} -type d -maxdepth 1 -mindepth 1) to get only the immediate child directory names. Then you can use basename to get only the directory name, or some ugly hack like | rev | cut -f1 -d/ | rev.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .