I am writing some scripts to check if the \"s\" permission bit is set for a particular file. For example - permissions for my file are as follows-
drwxr-s---
If you're using perl, then have a look at perldoc:
-u File has setuid bit set.
-g File has setgid bit set.
-k File has sticky bit set.
So something like:
if (-u $filename) { ... }
non-perl options
stat#!/bin/bash
check_file="/tmp/foo.bar";
touch "$check_file";
chmod g+s "$check_file";
if stat -L -c "%A" "$check_file" | cut -c7 | grep -E '^S$' > /dev/null; then
echo "File $check_file has setgid."
fi
Explanation:
stat to print the file permissions.cutgrep to check if the result is S (indicated setgid) and if so we do whatever we want with that file that has setgid.findI have found (hah hah) that find is quite useful for the purpose of finding stuff based on permissions.
find . -perm -g=s -exec echo chmod g-s "{}" \;
Finds all files/directories with setgid and unsets it.