Consider path in a directory structure


now if I want to list all path which has sub directory C in it, then can it be done through grep? Expected output:


I tried using grep and find but could not achieve this.


While ka3ak's answer works, find comes with a parameter "-path" so you can simply use

find . -type d -path "*/c/*"

-path seems also a bit faster:

[hexathos:~/test] $ time find . -regextype posix-extended -regex ".*/c/.*"

real    0m0,013s
user    0m0,010s
sys 0m0,000s
[hexathos:~/test] $ time find . -type d -path "*/c/*"

real    0m0,012s
user    0m0,007s
sys 0m0,003s

You only need find for this:

find A -type d -regextype posix-extended -regex ".*/C/.*"

For the following directory structure

└── B                                                                                                                                                                                                              
    ├── C                                                                                                                                                                                                          
    │   ├── D                                                                                                                                                                                                      
    │   └── E                                                                                                                                                                                                      
    ├── C1                                                                                                                                                                                                         
    │   └── E                                                                                                                                                                                                      
    └── O
        └── P

it would return:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.