0

I am trying to write a script which would retrieve the permissions of a group of files and directories. Then check each permission to see if they are set correctly.

If the permissions are not set correctly then I would like it to echo which directory or group of files permissions were incorrectly set.

So far what I have is, I have used 'find' to recursively find all files and directories inside a certain directory and then execute stat to return the current permissions. From the returned list of permissions I then use an if-then statement to check if any files or directories have different permissions then what are expected. This is accomplished by the != operator and using pattern matching. So all files should have permissions set to 444 and directories to 555 and if not return that the permissions are wrong.

for site in $(echo /var/www/*)
do
    permcheckfile=$(find $site -type f -exec stat -c '%a' '{}' +)
    permcheckdir=$(find $site -type d -exec stat -c '%a' '{}' +)

    if [[ $permcheckfile != *444 ]]
    then
        echo "$site file permissions are wrong"
    else
        echo "$site file permissions are correct"
    fi

    if [[ $permcheckdir != *555 ]]
    then 
        echo "$site directory permissions are wrong"
    else
        echo "$site directory permissions are correct"
    fi
 done

The problem found with the script above is sometimes it will return false positives and I have no idea why.

Does anyone have an idea where I am going wrong? Is there a better way of accomplishing what I am trying to achieve? Any help or suggestions would be greatly appreciated. Thank you for your time and help

0

2 Answers 2

1

You need to loop the permcheckfile and permcheckdir arrays.

for site in $(echo /var/www/*)
do 
    for file in $(find $site -type f -exec stat -c '%a' '{}' +)
    do
        if [[ $file != *444 ]]
        then
            echo "$site/$file permissions are wrong"
        else
            echo "$site/$file permissions are correct"
        fi
    done

    for dir in $(find $site -type d -exec stat -c '%a' '{}' +)
    do
        if [[ $dir != *555 ]]
        then
            echo "$site directory permissions are wrong"
        else
            echo "$site directory permissions are correct"
        fi
    done
 done
1
  • Thank you very much, this worked perfectly. The thought had crossed my mind to loop them, but I was unsure how to do so. Thanks
    – SamuelR
    Commented Feb 14, 2017 at 23:19
0

You can achieve all that you require with a single invocation of 'find', as shown below. It can be further optimized but at the cost of clarity.

#!/bin/sh
p=$(type -P printf)
site='/var/www'
cd "$site" && \
find . \
   \( -type f             -perm 444 -exec $p "$site/%s file permissions are correct.\n"      {} + \) -o \
   \( -type f           ! -perm 444 -exec $p "$site/%s file permissions are wrong.\n"        {} + \) -o \
   \( -type d ! -name . ! -perm 555 -exec $p "$site/%s directory permissions are wrong.\n"   {} + \) -o \
   \( -type d ! -name .   -perm 555 -exec $p "$site/%s directory permissions are correct.\n" {} + \)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .