Recursively read folders and executes command on each of them

后端 未结 7 1609
执笔经年
执笔经年 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:52

    Some basic shells miss commands like 'find' and some of their commands don't support recursivity. In that case you can use this script to run the desired command in all subdirs in the tree:

    CDIR=$(pwd)
    for i in $(ls -R | grep :); do
        DIR=${i%:}                    # Strip ':'
        cd $DIR
        $1                            # Your command
        cd $CDIR
    done
    

    If you name the above "recurse.sh" then use:

    ./recurse.sh 
    

    Example (change the owner/group to 'root' of all files in the tree):

    ./recurse.sh "chown 0:0 *"
    

提交回复
热议问题