Wednesday, November 3, 2010

recurperms

I wrote a script to remove the executable flags from all files within a directory but directories themsevles. It will also descend into sub-directories.

#!/bin/bash

# This script will chmod -x every file within a
# directory recursively, without removing the
# executable nature of the directories contained
# within.
################################################

do_recursion() {
    files=`ls`
    file=
    for i in $files; do
        file=$file$i
        if [[ ! -e $file ]]; then
            file=$file' '
            continue
        fi
        if [[ -d "$(pwd)/$file" ]]; then
            echo "descending into directory $(pwd)/$file"
            cd "$(pwd)/$file"
            do_recursion
        fi
        if [[ -d "$(pwd)/$file" ]]; then
            echo "ascending to directory $(pwd)/$file"
        else
            echo "chmodding $(pwd)/$file"
            chmod -x $file
        fi
        file=
    done
    cd ..
    return
}

do_recursion

exit 0

No comments:

Post a Comment