How to go to each directory and execute a command?

后端 未结 10 1005
無奈伤痛
無奈伤痛 2020-12-02 04:13

How do I write a bash script that goes through each directory inside a parent_directory and executes a command in each directory

10条回答
  •  温柔的废话
    2020-12-02 04:30

    If the toplevel folder is known you can just write something like this:

    for dir in `ls $YOUR_TOP_LEVEL_FOLDER`;
    do
        for subdir in `ls $YOUR_TOP_LEVEL_FOLDER/$dir`;
        do
          $(PLAY AS MUCH AS YOU WANT);
        done
    done
    

    On the $(PLAY AS MUCH AS YOU WANT); you can put as much code as you want.

    Note that I didn't "cd" on any directory.

    Cheers,

提交回复
热议问题