0

I would like to find a directory with name recursively up started from current directory and get it's absolute path. I don't know which directory contains what I need. So I have to check if each upper dir contains what I need.

For example I have such directory tree:

.
├── root
├── ...
├── someDir000
│   └── ...
│   └── someDir001
│       ├── otherDir000
│       └── dirPathIWantToFind
│       └── ...
├── someDir002
│   ├── anotherDir000
│   └── anotherDir001
│   ├── myCurrentDir

I'm currently in myCurrentDir and want to find a path for ** dirPathIWantToFind**, so it should be /root/someDir000/someDir001/dirPathIWantToFind.

I know it's possible to go recursively dip down from current dir, but how to do it UP?

andrey
  • 1,136
  • 9
  • 19

1 Answers1

2
Dir.glob(Pathname('/root').join('**/**/dirPathIWantToFind'))

See Dir.glob. Be warned though - this can take quite a bit of time depending on the size of your file system.

max
  • 96,212
  • 14
  • 104
  • 165
  • Thanks! It works, but I was wrong in question. I'm sorry. I changed it. – andrey May 07 '20 at 16:56
  • It might be a deep hierarchy with a lot of dirs inside if starts from root. Is it possible to do it opposite and starts from current directory? In my case it should be much shorter. I'm thinking about to change to upper dir, check if it contains what I need get the path, if not then change upper again. – andrey May 07 '20 at 17:11
  • 1
    You can use `Pathname#ascend` to walk up the directories and then call `Dir.glob(Pathname(current_path).join('**/**/dirPathIWantToFind'))` on each step until you find the directory. – max May 07 '20 at 18:29