Bash script to recursively obtain file and directory permissions, compare them to desired permissions and return if correct or not
0
votes
2
answers
2666
views
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.
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 unexpected permissions. 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.
Where I am going wrong? Is there a better way?
Asked by SamuelR
(3 rep)
Feb 14, 2017, 08:05 PM
Last activity: Dec 31, 2024, 12:34 PM
Last activity: Dec 31, 2024, 12:34 PM