有没有办法递归搜索嵌套的子目录并对每个子目录执行命令?

Is there a way to recursively search through nested subdirectories and execute a command on each one?

我找不到涉及嵌套子目录的问题。假设我有一个这样的目录大纲:

dir1
 |--dir2
 |    |--a
 |    |--b
 |    |--c
 |--dir3
 |    |--dir4
 |    |--file1
 |    |--file2
 |    |--file3
 |    |--dir5
 |        |--test1
 |        |--test2
 |        |--test3
 |--dir6
 |   |--fileA
 |   |--dir7
 |       |--fileB

假设我必须在这些目录和每个子目录中的每一个中执行一系列命令。以下是我需要我的函数执行的操作的概要:

有人可以为我提供 Bash 脚本函数来执行此操作吗?我是一个完全的初学者,这是我作业中给我带来最大麻烦的部分。预先感谢您的帮助!

递归函数,是你要找的吗?

#!/bin/bash

function loop {

    for dir in */; do # */ to matach only directories
        if [ -d "$dir" ]; then # check if directory has further directoies
            (cd "$dir" && pwd && loop); # replace pwd with the command to be executed in the directory
        fi 
    done
}

loop