Perform an action in every sub-directory using Bash

前端 未结 9 596
迷失自我
迷失自我 2020-12-04 05:46

I am working on a script that needs to perform an action in every sub-directory of a specific folder.

What is the most efficient way to write that?

9条回答
  •  爱一瞬间的悲伤
    2020-12-04 06:13

    This will create a subshell (which means that variable values will be lost when the while loop exits):

    find . -type d | while read -r dir
    do
        something
    done
    

    This won't:

    while read -r dir
    do
        something
    done < <(find . -type d)
    

    Either one will work if there are spaces in directory names.

提交回复
热议问题