How to go to each directory and execute a command?

后端 未结 10 1041
無奈伤痛
無奈伤痛 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:29

    You can achieve this by piping and then using xargs. The catch is you need to use the -I flag which will replace the substring in your bash command with the substring passed by each of the xargs.

    ls -d */ | xargs -I {} bash -c "cd '{}' && pwd"
    

    You may want to replace pwd with whatever command you want to execute in each directory.

提交回复
热议问题