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

时间:2021-01-02 21:23:12

标签: bash

大家好,我是 bash 新手,需要一些非常简单但不知道的东西。我需要一个 bash 脚本,我在管道中调用它来查找特定文件“terragrunt.hcl”。 bash 应该从目录 (A) 及其子目录(B、C、D、E 等...)开始查看,然后在每个“terragrunt.hcl”文件上执行 2 个命令,但从目录内部执行这 2 个命令文件所在的位置。

如果我要“手动”完成,它看起来像这样:

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),但它没有执行我正在钓鱼的内容。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

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

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

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

如果你想运行多个命令,你可以这样做:

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