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

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

问题描述

我怎样写一个bash脚本,通过每个目录进了parent_directory内和执行 命令每个目录

的目录结构是如下所示:


  

parent_directory(名称可以是任何东西 - 犯规遵循一个模式)


  
  

      
  • 001(目录名遵循这种模式)
      

        
    • 0001.txt(文件名遵循此模式)

    •   
    • 0002.txt

    •   
    • 0003.txt

    •   

  •   
  • 002
      

        
    • 0001.txt

    •   
    • 0002.txt

    •   
    • 0003.txt

    •   
    • 0004.txt

    •   

  •   
  • 003
      

        
    • 0001.txt

    •   

  •   

  
  

目录的数量是未知的。



解决方案

您可以执行以下操作,当你的当前目录是 parent_directory

 在D [0-9] [0-9] [0-9]

    (CD $ D&放大器;&安培;你的命令,在这里)
DONE

创建一个子shell,所以当前目录没有在主脚本改变。

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

The directory structure is as follows:

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

  • 001 (directory names follow this pattern)
    • 0001.txt (filenames follow this pattern)
    • 0002.txt
    • 0003.txt
  • 002
    • 0001.txt
    • 0002.txt
    • 0003.txt
    • 0004.txt
  • 003
    • 0001.txt

the number of directories is unknown.

解决方案

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

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

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

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