Perform an action in every sub-directory using Bash

前端 未结 9 604
迷失自我
迷失自我 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条回答
  •  旧时难觅i
    2020-12-04 06:10

    You could try:

    #!/bin/bash
    ### $1 == the first args to this script
    ### usage: script.sh /path/to/dir/
    
    for f in `find . -maxdepth 1 -mindepth 1 -type d`; do
      cd "$f"
      
    done
    

    or similar...

    Explanation:

    find . -maxdepth 1 -mindepth 1 -type d : Only find directories with a maximum recursive depth of 1 (only the subdirectories of $1) and minimum depth of 1 (excludes current folder .)

提交回复
热议问题