Method #1 - stat
You could use the stat
command to get the permissions bits. stat
is available on most Unixes (OSX, BSD, not AIX from what I can find). This should work on most Unixes, except OSX & BSD from what I can find.
$ stat -c "%a" <file>
Example
$ ls -l a
-rw-rw-r-- 1 saml saml 155 Oct 6 14:16 afile.txt
Use this command:
$ stat -c "%a" afile.txt
664
And use a simple grep
to see if the groups permissions mode is a 6 or 7.
$ stat -c "%a" afile.txt | grep ".[67]."
For OSX and BSD you'd need to use this form of stat
, stat -f
(or perhaps stat -x
), and parse accordingly. Since the options to stat
are different you could wrap this command in an lsb_release -a
command and call the appropriate version based on the OS. Not ideal but workable. Realize that lsb_release
is only available for Linux distros so another alternative would need to be devised for testing other Unix OSes.
Method #2 - find
I think this command might serve you better though. I makes use of find
and the printf
switch.
Example
$ find a -prune -printf '%m\n'
664
Method #3 - Perl
Perl might be a more portable way to do this depending on the OSes you're trying to cover.
$ perl -le '@pv=stat("afile.txt"); printf "%04o", $pv[2] & 07777;'
0664
NOTE: The above makes use of Perl's stat()
function to query the filesystem bits.
You can make this more compact by forgoing using an array, @pv
, and dealing with the output of stat()
directly:
$ perl -le 'printf "%04o", (stat("a"))[2] & 07777;'
0664