Recursively read folders and executes command on each of them

后端 未结 7 1640
执笔经年
执笔经年 2020-12-12 21:05

I am trying to recurse into folders and then run commands on them, using bash script. Any suggestions?

7条回答
  •  不知归路
    2020-12-12 21:49

    Bash 4.0 introduced the globstar option, so a construct like:

    for f in mydir/**/*
    do
      # operations here
    done
    

    ...will act recursively on whatever lands in $f. Turn this on with "shopt -s globstar", otherwise the ** will be treated as a singular *.

    Found this gem today at http://www.linuxjournal.com/content/globstar-new-bash-globbing-option, after being inspired by the zsh construct (which I have enabled by default).

提交回复
热议问题