Search code examples
shellfile-permissions

How do I check permissions of a directory and all of its recursive content?


I created a simple bash script that checks if a directory has the given permissions, then executes its task.

if [ `stat -c "%a" my_directory` != 777 ]; then
    # script to be executed
fi

Now, this works well, but it only checks for permissions of the given directory. I also need to check recursively for all of the content of my_directory if the permissions are set to something different than 777.

In other words, this script should run as long as even a single file or directory inside my_directory has its permissions not set as 777. If everything inside this directory is completely accessible, the script should not run.

How can I modify my script to make it work this way?


Solution

  • Thanks to Jordanm's suggestion, I modified my script like this

    checkfiles=0
    declare -i checkfiles
    
    for file in `find my_directory ! -perm 777`
    
    do
    
        checkfiles+=1
    
    done
    
    if [ $checkfiles != 0 ]; then
    
        # script to be executed
        
    fi