Perform an action in every sub-directory using Bash

前端 未结 9 590
迷失自我
迷失自我 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:22
    for D in `find . -type d`
    do
        //Do whatever you need with D
    done
    
    0 讨论(0)
  • 2020-12-04 06:26

    find . -type d -print0 | xargs -0 -n 1 my_command

    0 讨论(0)
  • 2020-12-04 06:31

    the accepted answer will break on white spaces if the directory names have them, and the preferred syntax is $() for bash/ksh. Use GNU find -exec option with +; eg

    find .... -exec mycommand +; #this is same as passing to xargs

    or use a while loop

    find .... |  while read -r D
    do
      ...
    done 
    
    0 讨论(0)
提交回复
热议问题