Home > Mobile >  How to traverse dirs in linux and cd into a specific dir
How to traverse dirs in linux and cd into a specific dir

Time:01-26

I have a file structure as following:

/home/myhome/me/staging/15/1234/my_stats/

/home/myhome/me/staging/16/5678/my_stats/

/home/myhome/me/staging/17/7890/my_stats/

/home/myhome/me/staging/18/3456/my_stats/

I need to travel to the dir "my_stats" and execute query to find files in my cmd. There are multiple dirs in "staging" and I need to go into every one of them and check if 'my_stats' dir exists. If it exists, then I need to run a cmd query in "my_stats" dir.

The dir structure will always be in the following format:

/home/myhome/me/staging/<2 digit name>/<4 digit name>/my_stats/

I have tried iterating through the structure using a nested for loop and checking all dirs in 'staging' which is proving to be slow. Is there a way to using the 'find' command with 'depth' to do the same? Or can we implement this with pattern matching ?

Appreciate the help. Thanks!

CodePudding user response:

found the answer! we can use * for it.

/home/myhome/me/staging/*/**/my_stats/*

Will try to find a better solution which can maybe use len of dir to better differentiate it

CodePudding user response:

Try this one

find . -type f -path "./[0-9][0-9]/[0-9][0-9][0-9][0-9]/my_stats/*"

can replace the . to your own path.

  • Related