Bash脚本在目录及其子目录中查找特定文件,并在该特定文件上执行2个命令



大家好,我是bash的新手,需要一些非常简单但无法理解的东西。我需要一个在管道中调用的bash脚本来查找特定的文件"terragrumt.hcl";。bash应该从目录(a(及其子目录(B、C、D、E等(开始查找,然后在每个"上执行2个命令;terragrumt.hcl";文件BUT从文件所在的目录中执行这两个命令。

如果我要这么做";"手工";,它看起来像这样:

cd A
(if terragrunt.hcl exist execute **command 1 && command 2** on terragrunt.hcl)
cd ../B
(if terragrunt.hcl exist execute **command 1 && command 2** on terragrunt.hcl)
cd ../C
(if terragrunt.hcl exist execute **command 1 && command 2** on terragrunt.hcl)

到目前为止,我并没有发现任何真正有用的东西。我在网上找到的(https://unix.stackexchange.com/questions/295965/how-to-use-find-exec-to-execute-command-in-directory-of-found-file-not-curre)但它并没有实现我所追求的。任何帮助都将不胜感激。

如果您的平台的find支持它,请尝试使用-exedir

find . -name 'terragrunt.hcl' -execdir pwd ;

pwd替换为要从该目录运行的命令。

如果你想运行多个命令,你可以做一些类似的事情:

find . -name 'terragrunt.hcl' -execdir sh -c "command 1 && command 2" ;

最新更新