如何进入每个目录并执行命令? [英] How to go to each directory and execute a command?

查看:20
本文介绍了如何进入每个目录并执行命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写一个 bash 脚本,该脚本遍历 parent_directory 中的每个目录并执行每个目录中的命令.

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

目录结构如下:

parent_directory(名称可以是任何东西 - 不遵循模式)

parent_directory (name could be anything - doesnt follow a pattern)

  • 001(目录名称遵循此模式)
    • 0001.txt(文件名遵循此模式)
    • 0002.txt
    • 0003.txt
    • 0001.txt
    • 0002.txt
    • 0003.txt
    • 0004.txt
    • 0001.txt

    目录数量未知.

    推荐答案

    当你当前的目录是 parent_directory 时,你可以进行以下操作:

    You can do the following, when your current directory is parent_directory:

    for d in [0-9][0-9][0-9]
    do
        ( cd "$d" && your-command-here )
    done
    

    () 创建一个子shell,所以当前目录不会在主脚本中改变.

    The ( and ) create a subshell, so the current directory isn't changed in the main script.

    这篇关于如何进入每个目录并执行命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆