jcubic
jcubic

Reputation: 66560

How to search back in directory tree on GNU/Linux for a file

I have few repositories and each have config.php file that contain information about database like password. I have script that connect to database using stuff from that file and need to search back in directory tree so I can call it in every nested directory.

[ -e config.php ] && $(php -r 'require("config.php"); echo "mysql -u".$config["db_user"] . " -p".$config["db_pass"] . " " . $config["db_name"];')

What is easiest way to search back in directory tree using Bash?

Upvotes: 2

Views: 644

Answers (2)

user1666959
user1666959

Reputation: 1855

This chops off a directory from the end of PWD until it becomes empty. Some care may need to be taken in the presence of links.

S="${PWD}"
while [ -n "${S}" ]
do
    [ -e "config.php" ] && ls $S/
    S=${S%/*}
done

Upvotes: 2

user1666959
user1666959

Reputation: 1855

'find topdir/ -type f -iname "config.php"' where 'topdir' is the uppermost directory where your config scripts reside. The depending on your requirements you may want to process the 'config.php' files in one go using

'find topdir/ -type f -iname "config.php" -print0 | xargs -0 yourscript'

or one by one 'find topdir/ -type f -iname "config.php" -exec yourscript {} \;'

Upvotes: 0

Related Questions